Skip to content

Commit 488e8e9

Browse files
author
Hirundo
committed
Change #2877: Make engine_override(), deactiavate() and import_sound() accept grfids as strings only.
1 parent 68242b5 commit 488e8e9

15 files changed

+40
-82
lines changed

docs/nml-language.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,13 @@ <h2><a name="block-disable_item">Disable items</a></h2>
10481048
</pre>
10491049

10501050
<h2><a name="block-deactivate">Deactivate other NewGRFs</a></h2>
1051+
<pre class="exl">
1052+
deactivate(&lt;grfID&gt;[[, &lt;grfID2&gt;], ...]);
1053+
</pre>
1054+
<p>With this statement, you can deactivate other NewGRFs. As parameters, you
1055+
can supply any number of grfids (4-byte strings) to deactivate. Note that using
1056+
this feature to block incompatible grfs is highly discouraged. Instead, disable
1057+
your own grf with an error message via the <code>error()</code> statement.</p>
10511058

10521059
<h2><a name="block-othergrf">Testing for other NewGRFs</a></h2>
10531060
See the <a href="nml-language.html#builtin-functions">builtin functions</a> for the functions grf_XXX.
@@ -1076,7 +1083,7 @@ <h2><a name="block-override-grf">Overriding vehicles from other NewGRFs</a></h2>
10761083
</pre>
10771084
The targetID must be the grfID of the NewGRF whose vehicles are supposed to be changed via the engine override.
10781085
The sourceID is the grfID of the NewGRF defining the overriding vehicles. If not set explicitly, the grfID of
1079-
the NewGRF containing this statement is used.
1086+
the NewGRF containing this statement is used. Both grfIDs are 4-byte strings.
10801087
</p>
10811088
<h2><a name="block-replacement">Sprite replacement</a></h2>
10821089
<p>This section covers the three available methods to replace sprites.

nml/actions/action0.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ def get_disable_actions(disable):
465465

466466
class EngineOverrideProp(object):
467467
def __init__(self, source, target):
468-
self.source = actionE.bswap32(source.value)
469-
self.target = actionE.bswap32(target.value)
468+
self.source = source
469+
self.target = target
470470

471471
def write(self, file):
472472
file.print_bytex(0x11)
@@ -478,11 +478,9 @@ def get_size(self):
478478
return 9
479479

480480
def get_engine_override_action(override):
481-
source = override.source_grfid.reduce_constant()
482-
target = override.grfid.reduce_constant()
483481
act0 = Action0(0x08, 0)
484482
act0.num_ids = 1
485-
act0.prop_list.append(EngineOverrideProp(source, target))
483+
act0.prop_list.append(EngineOverrideProp(override.source_grfid, override.grfid))
486484
return [act0]
487485

488486
def get_callback_flags_actions(feature, id, flags):

nml/actions/actionE.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class ActionE(base_action.BaseAction):
55
def __init__(self, grfid_list):
6-
self.grfid_list = [bswap32(grfid.value) for grfid in grfid_list]
6+
self.grfid_list = grfid_list
77

88
def write(self, file):
99
size = 2 + 4 * len(self.grfid_list)
@@ -16,34 +16,6 @@ def write(self, file):
1616
file.newline()
1717
file.end_sprite()
1818

19-
def bswap32(value):
20-
return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) | ((value & 0xFF0000) >> 8) | ((value & 0xFF000000) >> 24)
21-
2219
def parse_deactivate_block(block):
23-
action6.free_parameters.save()
24-
grfid_list = []
25-
action_list = []
26-
act6 = action6.Action6()
27-
offset = 2
28-
for grfid in block.grfid_list:
29-
if isinstance(grfid, expression.ConstantNumeric):
30-
grfid_list.append(grfid)
31-
else:
32-
tmp_param, tmp_param_actions = actionD.get_tmp_parameter(grfid)
33-
action_list.extend(tmp_param_actions)
34-
for i in range(0, 4):
35-
if i == 0:
36-
param = tmp_param
37-
else:
38-
param = action6.free_parameters.pop()
39-
action_list.append(actionD.ActionD(expression.ConstantNumeric(param), expression.ConstantNumeric(tmp_param),
40-
nmlop.SHIFTU_LEFT, expression.ConstantNumeric(0xFF), expression.ConstantNumeric(-8 * i)))
41-
act6.modify_bytes(param, 1, offset + 3 - i)
42-
grfid_list.append(expression.ConstantNumeric(0))
43-
offset += 4
44-
45-
if len(act6.modifications) != 0: action_list.append(act6)
46-
action_list.append(ActionE(grfid_list))
20+
return [ActionE(block.grfid_list)]
4721

48-
action6.free_parameters.restore()
49-
return action_list

nml/ast/deactivate.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from nml.actions import actionE
22
from nml.ast import base_statement
3+
from nml import expression
34

45
class DeactivateBlock(base_statement.BaseStatement):
56
def __init__(self, grfid_list, pos):
67
base_statement.BaseStatement.__init__(self, "deactivate()", pos)
78
self.grfid_list = grfid_list
89

910
def pre_process(self):
10-
self.grfid_list = [grfid.reduce() for grfid in self.grfid_list]
11+
# Parse (string-)expressions to integers
12+
self.grfid_list = [expression.parse_string_to_dword(grfid.reduce()) for grfid in self.grfid_list]
1113

1214
def debug_print(self, indentation):
1315
print indentation*' ' + 'Deactivate other newgrfs:'

nml/ast/override.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class EngineOverride(base_statement.BaseStatement):
77
AST Node for an engine override.
88
99
@ivar grfid: GRFid of the grf to override the engines from.
10-
@type grfid: L{Expression}
10+
@type grfid: C{int{
1111
1212
@ivar source_grfid: GRFid of the grf that overrides the engines.
13-
@type source_grfid: L{Expression} or C{None}
13+
@type source_grfid: C{int}
1414
"""
1515
def __init__(self, args, pos):
1616
base_statement.BaseStatement.__init__(self, "engine_override()", pos)
@@ -21,16 +21,15 @@ def pre_process(self):
2121
raise generic.ScriptError("engine_override expects 1 or 2 parameters", self.pos)
2222

2323
if len(self.args) == 1:
24-
source = expression.Identifier('GRFID')
24+
try:
25+
self.source_grfid = expression.Identifier('GRFID').reduce(global_constants.const_list).value
26+
assert isinstance(self.source_grfid, int)
27+
except generic.ScriptError:
28+
raise generic.ScriptError("GRFID of this grf is required, but no grf-block is defined.", self.pos)
2529
else:
26-
source = self.args[0]
27-
self.source_grfid = source.reduce(global_constants.const_list)
28-
if isinstance(self.source_grfid, expression.StringLiteral):
29-
self.source_grfid = expression.ConstantNumeric(expression.parse_string_to_dword(self.source_grfid))
30-
31-
self.grfid = self.args[-1].reduce(global_constants.const_list)
32-
if isinstance(self.grfid, expression.StringLiteral):
33-
self.grfid = expression.ConstantNumeric(expression.parse_string_to_dword(self.grfid))
30+
self.source_grfid = expression.parse_string_to_dword(self.args[0].reduce(global_constants.const_list))
31+
32+
self.grfid = expression.parse_string_to_dword(self.args[-1].reduce(global_constants.const_list))
3433

3534
def debug_print(self, indentation):
3635
print indentation*' ' + 'Engine override'

nml/expression/functioncall.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,7 @@ def builtin_sound_file(name, args, pos):
402402
def builtin_sound_import(name, args, pos):
403403
if len(args) != 2:
404404
raise generic.ScriptError(name + "() must have 2 parameter", pos)
405-
grfid = args[0].reduce()
406-
if isinstance(grfid, ConstantNumeric):
407-
grfid = grfid.value
408-
else:
409-
grfid = parse_string_to_dword(grfid)
405+
grfid = parse_string_to_dword(args[0].reduce())
410406
sound_num = args[1].reduce_constant().value
411407
return ConstantNumeric(action11.add_sound((grfid, sound_num)), pos)
412408
#}

regression/002_sounds.nml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
param[0] = sound("beef.wav");
2-
param[1] = import_sound(0x87654321, 3);
2+
param[1] = import_sound("\12\34\56\78", 3);

regression/004_deactivate.nml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Test deactivate(grfids...), which deactivates other grfs
33
The nfo / grf equivalent of this is ActionE
4-
Note that grfs are in big-endian, therefore byte-swapping is required
5-
If the grfid is not a compile-time constant, this is done at runtime.
64
*/
7-
deactivate(0x12345678, param[0]);
5+
deactivate("ABCD");
6+
deactivate("\01\02\03\04", "EFGH");
7+

regression/023_engine_override.nml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ grf {
1010
engine_override("test", "ABCD");
1111

1212
// also override some other grf, this time not setting our own grfid explicitly
13-
engine_override(0x12345678);
13+
engine_override("\12\34\56\78");

regression/expected/002_sounds.grf

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)