Skip to content

Commit c9bf5ef

Browse files
committed
impl NetworkCallable
1 parent d5a2778 commit c9bf5ef

4 files changed

Lines changed: 22 additions & 6 deletions

File tree

‎src/men-sharp-codegen/src/generator/network.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,12 @@ impl<'a, 'ast> Generator<'a, 'ast> {
192192
.map(|list| list.arguments)
193193
.unwrap_or(&[])
194194
{
195-
// `MaxEventsPerSecond = value` sets the attribute's
195+
// `maxEventsPerSecond: 7` names the constructor's
196+
// parameter, `MaxEventsPerSecond = 7` would set a
196197
// property; the one positional argument is the rate too
198+
let names_the_parameter = argument.name.as_ref().is_some_and(|name| {
199+
matches!(name.value, "MaxEventsPerSecond" | "maxEventsPerSecond")
200+
});
197201
let (expression, named) = match &argument.value {
198202
ArgumentValue::Expression(Expression::Assignment(assignment)) => {
199203
let target = match &assignment.target {
@@ -212,7 +216,9 @@ impl<'a, 'ast> Generator<'a, 'ast> {
212216
_ => continue,
213217
}
214218
}
215-
ArgumentValue::Expression(expression) => (Some(expression), false),
219+
ArgumentValue::Expression(expression) => {
220+
(Some(expression), names_the_parameter)
221+
}
216222
_ => (None, false),
217223
};
218224
if !named && argument.name.is_some() {

‎src/men-sharp-compiler/tests/codegen.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16636,8 +16636,9 @@ const NETWORK_CALLABLE_BASE: &str = r#"
1663616636
#[test]
1663716637
fn a_network_callables_rate_is_any_integer_constant_under_any_spelling() {
1663816638
// issue: only a literal rate was read — a `const`, `3 + 4` or `0x7`
16639-
// silently fell back to the default; and `[NC]` through a using alias
16640-
// was not recognised at all, so the event lost its metadata
16639+
// silently fell back to the default (the named form
16640+
// `maxEventsPerSecond: 7` too); and `[NC]` through a using alias was
16641+
// not recognised at all, so the event lost its metadata
1664116642
let Some(program) = compile_behaviour(
1664216643
vec![
1664316644
SourceCode::new("base.cs", NETWORK_CALLABLE_BASE),
@@ -16660,6 +16661,8 @@ fn a_network_callables_rate_is_any_integer_constant_under_any_spelling() {
1666016661
[NetworkCallableAttribute(Rate)] public void Suffixed(int x) { }
1666116662
[NC] public void Aliased(int x) { }
1666216663
[NC(Rate)] public void AliasedRate(int x) { }
16664+
[NetworkCallable(maxEventsPerSecond: 7)] public void Colon(int x) { }
16665+
[NC(maxEventsPerSecond: Rate)] public void AliasedColon(int x) { }
1666316666
}
1666416667
}
1666516668
"#,
@@ -16684,6 +16687,8 @@ fn a_network_callables_rate_is_any_integer_constant_under_any_spelling() {
1668416687
"Qualified",
1668516688
"Suffixed",
1668616689
"AliasedRate",
16690+
"Colon",
16691+
"AliasedColon",
1668716692
] {
1668816693
assert!(
1668916694
meta.contains(&format!(

‎tests/unity-project/Assets/MenSharp/VerifyNetwork.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ public void Aim(Vector3 at, int times)
3939
Debug.Log($"[verify-net] Aim ran: at={at}, times={times}");
4040
}
4141

42-
// rates from constants and through an alias: the metadata the
43-
// integration test reads back from the program asset carries 7 for each
42+
// rates from constants, by parameter name, and through an alias: the
43+
// metadata the integration test reads back from the program asset
44+
// carries 7 for each
4445
private const int Rate = 7;
4546

4647
[NetworkCallable(Rate)]
4748
public void Rated(int value) { }
4849

50+
[NetworkCallable(maxEventsPerSecond: Rate)]
51+
public void Named(int value) { }
52+
4953
[NC(0x7)]
5054
public void Aliased(int value) { }
5155

‎tests/unity-project/Assets/Tests/Editor/MenSharpIntegrationTests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ public void NetworkCallableRatesComeFromConstantsAndAliases()
667667
rates[callable.@event] = callable.maxEventsPerSecond;
668668
}
669669
Assert.AreEqual(7, rates["Rated"]);
670+
Assert.AreEqual(7, rates["Named"]);
670671
Assert.AreEqual(7, rates["Aliased"]);
671672
Assert.AreEqual(0, rates["AliasedDefault"]);
672673
Assert.AreEqual(5, rates["Aim"]);

0 commit comments

Comments
 (0)