Skip to content

Commit d99f995

Browse files
committed
up-to-date
- WeaponName fixes - DestroyEvent + ParallelTimers
1 parent d113b25 commit d99f995

File tree

4 files changed

+52
-120
lines changed

4 files changed

+52
-120
lines changed

IronPythonModule/FougeriteEx/IPTimedEvent.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ public class IPTimedEvent {
1313
private long lastTick;
1414
private int _elapsedCount;
1515

16-
public delegate void TimedEventFireDelegate(string name);
17-
public delegate void TimedEventFireArgsDelegate(string name, Dictionary<string, object> list);
16+
public delegate void TimedEventFireDelegate(IPTimedEvent evt);
1817

1918
public event TimedEventFireDelegate OnFire;
20-
public event TimedEventFireArgsDelegate OnFireArgs;
2119

2220
public IPTimedEvent(string name, double interval) {
2321
this._name = name;
@@ -34,10 +32,7 @@ public IPTimedEvent(string name, double interval, Dictionary<string, object> arg
3432

3533
private void _timer_Elapsed(object sender, ElapsedEventArgs e) {
3634
if (this.OnFire != null) {
37-
this.OnFire(this.Name);
38-
}
39-
if (this.OnFireArgs != null) {
40-
this.OnFireArgs(this.Name, this.Args);
35+
this.OnFire(this);
4136
}
4237

4338
this._elapsedCount += 1;
@@ -53,6 +48,11 @@ public void Stop() {
5348
this._timer.Stop();
5449
}
5550

51+
public void Kill() {
52+
this._timer.Stop();
53+
this._timer.Dispose();
54+
}
55+
5656
public Dictionary<string, object> Args {
5757
get { return this._args; }
5858
set { this._args = value; }

IronPythonModule/IPModule.cs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,6 @@ public static void ConsoleReceived(ref ConsoleSystem.Arg arg, bool external) {
6565
if (OnConsoleReceived != null)
6666
OnConsoleReceived(ref arg, external);
6767
}
68-
// EntityDestroy
69-
public static event IPModule.EntityDestroyedDelegate OnEntityDestroyed;
70-
71-
public delegate void EntityDestroyedDelegate(Events.DestroyEvent de);
72-
// EntityHurt
73-
public static event IPModule.EntityHurtDelegate OnEntityHurt;
74-
75-
public delegate void EntityHurtDelegate(Fougerite.Events.HurtEvent he);
76-
77-
public static void EntityHurt(Fougerite.Events.HurtEvent he) {
78-
if (he.DamageEvent.status != LifeStatus.IsAlive) {
79-
DamageEvent dmgEvt = he.DamageEvent;
80-
Events.DestroyEvent de = new Events.DestroyEvent(ref dmgEvt, he.Entity, he.IsDecay);
81-
if (OnEntityDestroyed != null)
82-
OnEntityDestroyed(de);
83-
return;
84-
}
85-
86-
if (he.IsDecay)
87-
return; // NOTE: this should be done in Fougerite.Hooks imo
88-
89-
if (OnEntityHurt != null)
90-
OnEntityHurt(he);
91-
}
92-
93-
9468
#endregion
9569

9670
#region Init/Deinit
@@ -102,8 +76,6 @@ public override void Initialize() {
10276
}
10377
plugins = new Dictionary<string, IPPlugin>();
10478
ReloadPlugins();
105-
Hooks.OnEntityHurt -= new Hooks.EntityHurtDelegate(EntityHurt);
106-
Hooks.OnEntityHurt += new Hooks.EntityHurtDelegate(EntityHurt);
10779
Hooks.OnConsoleReceived -= new Hooks.ConsoleHandlerDelegate(ConsoleReceived);
10880
Hooks.OnConsoleReceived += new Hooks.ConsoleHandlerDelegate(ConsoleReceived);
10981
if (instance == null)
@@ -112,7 +84,6 @@ public override void Initialize() {
11284

11385
public override void DeInitialize() {
11486
UnloadPlugins();
115-
Hooks.OnEntityHurt -= new Hooks.EntityHurtDelegate(EntityHurt);
11687
}
11788

11889
public static IPModule GetInstance() {
@@ -237,9 +208,9 @@ private void InstallHooks(IPPlugin plugin) {
237208
case "On_PlayerSpawn": Hooks.OnPlayerSpawning += new Hooks.PlayerSpawnHandlerDelegate(plugin.OnPlayerSpawn); break;
238209
case "On_PlayerSpawned": Hooks.OnPlayerSpawned += new Hooks.PlayerSpawnHandlerDelegate(plugin.OnPlayerSpawned); break;
239210
case "On_PlayerGathering": Hooks.OnPlayerGathering += new Hooks.PlayerGatheringHandlerDelegate(plugin.OnPlayerGathering); break;
240-
case "On_EntityHurt": OnEntityHurt += new EntityHurtDelegate(plugin.OnEntityHurt); break;
211+
case "On_EntityHurt": Hooks.OnEntityHurt += new Hooks.EntityHurtDelegate(plugin.OnEntityHurt); break;
241212
case "On_EntityDecay": Hooks.OnEntityDecay += new Hooks.EntityDecayDelegate(plugin.OnEntityDecay); break;
242-
case "On_EntityDestroyed": OnEntityDestroyed += new EntityDestroyedDelegate(plugin.OnEntityDestroyed); break;
213+
case "On_EntityDestroyed": Hooks.OnEntityDestroyed += new Hooks.EntityDestroyedDelegate(plugin.OnEntityDestroyed); break;
243214
case "On_EntityDeployed": Hooks.OnEntityDeployed += new Hooks.EntityDeployedDelegate(plugin.OnEntityDeployed); break;
244215
case "On_NPCHurt": Hooks.OnNPCHurt += new Hooks.HurtHandlerDelegate(plugin.OnNPCHurt); break;
245216
case "On_NPCKilled": Hooks.OnNPCKilled += new Hooks.KillHandlerDelegate(plugin.OnNPCKilled); break;
@@ -274,7 +245,7 @@ private void RemoveHooks(IPPlugin plugin) {
274245
case "On_PlayerGathering": Hooks.OnPlayerGathering -= new Hooks.PlayerGatheringHandlerDelegate(plugin.OnPlayerGathering); break;
275246
case "On_EntityHurt": Hooks.OnEntityHurt -= new Hooks.EntityHurtDelegate(plugin.OnEntityHurt); break;
276247
case "On_EntityDecay": Hooks.OnEntityDecay -= new Hooks.EntityDecayDelegate(plugin.OnEntityDecay); break;
277-
case "On_EntityDestroyed": IPModule.OnEntityDestroyed -= new IPModule.EntityDestroyedDelegate(plugin.OnEntityDestroyed); break;
248+
case "On_EntityDestroyed": Hooks.OnEntityDestroyed -= new Hooks.EntityDestroyedDelegate(plugin.OnEntityDestroyed); break;
278249
case "On_EntityDeployed": Hooks.OnEntityDeployed -= new Hooks.EntityDeployedDelegate(plugin.OnEntityDeployed); break;
279250
case "On_NPCHurt": Hooks.OnNPCHurt -= new Hooks.HurtHandlerDelegate(plugin.OnNPCHurt); break;
280251
case "On_NPCKilled": Hooks.OnNPCKilled -= new Hooks.KillHandlerDelegate(plugin.OnNPCKilled); break;

IronPythonModule/IPPlugin.cs

Lines changed: 39 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class IPPlugin {
2323
public readonly IList<string> Globals;
2424

2525
public readonly Dictionary<string, IPTimedEvent> Timers;
26+
public readonly List<IPTimedEvent> ParallelTimers;
2627

2728
public IPPlugin(string name, string code, DirectoryInfo path) {
2829
Name = name;
@@ -36,7 +37,7 @@ public IPPlugin(string name, string code, DirectoryInfo path) {
3637
Scope.SetVariable("Server", Fougerite.Server.GetServer());
3738
Scope.SetVariable("DataStore", DataStore.GetInstance());
3839
Scope.SetVariable("Util", Util.GetUtil());
39-
Scope.SetVariable("Entities", new LookUp());
40+
// Scope.SetVariable("Structures", new LookUp.Structures());
4041
Scope.SetVariable("World", World.GetWorld());
4142
Engine.Execute(code, Scope);
4243
Class = Engine.Operations.Invoke(Scope.GetVariable(name));
@@ -50,7 +51,7 @@ public void Invoke(string func, params object[] obj) {
5051
else
5152
Fougerite.Logger.LogDebug("[IronPython] Function: " + func + " not found in plugin: " + Name);
5253
} catch (Exception ex) {
53-
Fougerite.Logger.LogException(ex);
54+
Fougerite.Logger.LogError(Engine.GetService<ExceptionOperations>().FormatException(ex));
5455
}
5556
}
5657

@@ -88,21 +89,6 @@ public bool CreateDir(string path) {
8889
return false;
8990
}
9091

91-
// there is no json yet
92-
public void ToJsonFile(string path, string json) {
93-
path = ValidateRelativePath(path + ".json");
94-
File.WriteAllText(path, json);
95-
}
96-
97-
public string FromJsonFile(string path) {
98-
string json = @"";
99-
path = ValidateRelativePath(path + ".json");
100-
if (File.Exists(path))
101-
json = File.ReadAllText(path);
102-
103-
return json;
104-
}
105-
10692
// Dumper methods
10793
public bool DumpObjToFile(string path, object obj, string prefix = "") {
10894
return DumpObjToFile(path, obj, 1, 30, false, false, prefix);
@@ -311,26 +297,18 @@ public void OnDoorUse(Fougerite.Player player, DoorEvent evt) {
311297
}
312298

313299
public void OnEntityDecay(DecayEvent evt) {
314-
// CONSIDER: if(!evt.Entity...IsAlive) return;
315-
316300
this.Invoke("On_EntityDecay", new object[] { evt });
317301
}
318302

319303
public void OnEntityDeployed(Fougerite.Player player, Entity entity) {
320304
this.Invoke("On_EntityDeployed", new object[] { player, entity });
321305
}
322306

323-
public void OnEntityDestroyed(Events.DestroyEvent evt) {
324-
if (evt.WeaponName == null) {
325-
evt.WeaponName = fixWeaponName(evt.DamageEvent);
326-
}
307+
public void OnEntityDestroyed(DestroyEvent evt) {
327308
this.Invoke("On_EntityDestroyed", new object[] { evt });
328309
}
329310

330311
public void OnEntityHurt(HurtEvent evt) {
331-
if (evt.WeaponName == null) {
332-
evt.WeaponName = fixWeaponName(evt.DamageEvent);
333-
}
334312
this.Invoke("On_EntityHurt", new object[] { evt });
335313
}
336314

@@ -339,17 +317,11 @@ public void OnItemsLoaded(ItemsBlocks items) {
339317
}
340318

341319
public void OnNPCHurt(HurtEvent evt) {
342-
if (evt.WeaponName == null) {
343-
evt.WeaponName = fixWeaponName(evt.DamageEvent);
344-
}
345320
DumpObjToFile("NPCHurt", evt, 3, 20, true, true);
346321
this.Invoke("On_NPCHurt", new object[] { evt });
347322
}
348323

349324
public void OnNPCKilled(DeathEvent evt) {
350-
if (evt.WeaponName == null) {
351-
evt.WeaponName = fixWeaponName(evt.DamageEvent);
352-
}
353325
DumpObjToFile("NPCKilled", evt, 3, 20, true, true);
354326
this.Invoke("On_NPCKilled", new object[] { evt });
355327
}
@@ -367,16 +339,10 @@ public void OnPlayerGathering(Fougerite.Player player, GatherEvent evt) {
367339
}
368340

369341
public void OnPlayerHurt(HurtEvent evt) {
370-
if (evt.WeaponName == null) {
371-
evt.WeaponName = fixWeaponName(evt.DamageEvent, evt.DamageType);
372-
}
373342
this.Invoke("On_PlayerHurt", new object[] { evt });
374343
}
375344

376345
public void OnPlayerKilled(DeathEvent evt) {
377-
if (evt.WeaponName == null) {
378-
evt.WeaponName = fixWeaponName(evt.DamageEvent, evt.DamageType);
379-
}
380346
this.Invoke("On_PlayerKilled", new object[] { evt });
381347
}
382348

@@ -396,17 +362,9 @@ public void OnServerShutdown() {
396362
this.Invoke("On_ServerShutdown", new object[0]);
397363
}
398364

399-
// timer hooks
400-
401-
public void OnTimerCB(string name) {
402-
if (Globals.Contains(name + "Callback")) {
403-
Invoke(name + "Callback", new object[0]);
404-
}
405-
}
406-
407-
public void OnTimerCBArgs(string name, Dictionary<string, object> args) {
408-
if (Globals.Contains(name + "Callback")) {
409-
Invoke(name + "Callback", args);
365+
public void OnTimerCB(IPTimedEvent evt) {
366+
if (Globals.Contains(evt.Name + "Callback")) {
367+
Invoke(evt.Name + "Callback", evt);
410368
}
411369
}
412370

@@ -429,7 +387,7 @@ public IPTimedEvent CreateTimer(string name, int timeoutDelay, Dictionary<string
429387
if (timedEvent == null) {
430388
timedEvent = new IPTimedEvent(name, (double)timeoutDelay);
431389
timedEvent.Args = args;
432-
timedEvent.OnFireArgs += new IPTimedEvent.TimedEventFireArgsDelegate(OnTimerCBArgs);
390+
timedEvent.OnFire += new IPTimedEvent.TimedEventFireDelegate(OnTimerCB);
433391
Timers.Add(name, timedEvent);
434392
}
435393
return timedEvent;
@@ -447,47 +405,53 @@ public IPTimedEvent GetTimer(string name) {
447405

448406
public void KillTimer(string name) {
449407
IPTimedEvent timer = GetTimer(name);
450-
if (timer != null)
408+
if (timer == null)
451409
return;
452410

453-
timer.Stop();
411+
timer.Kill();
454412
Timers.Remove(name);
455413
}
456414

457415
public void KillTimers() {
458416
foreach (IPTimedEvent current in Timers.Values) {
459-
current.Stop();
417+
current.Kill();
418+
}
419+
foreach (IPTimedEvent timer in ParallelTimers) {
420+
timer.Kill();
460421
}
461422
Timers.Clear();
423+
ParallelTimers.Clear();
462424
}
463425

464426
#endregion
465427

466-
// temporarly, for my laziness
467-
public Dictionary<string, object> CreateDict() {
468-
return new Dictionary<string, object>();
428+
#region ParalellTimers
429+
430+
public IPTimedEvent CreateParallelTimer(string name, int timeoutDelay, Dictionary<string, object> args) {
431+
IPTimedEvent timedEvent = new IPTimedEvent(name, (double)timeoutDelay);
432+
timedEvent.Args = args;
433+
timedEvent.OnFire += new IPTimedEvent.TimedEventFireDelegate(OnTimerCB);
434+
ParallelTimers.Add(timedEvent);
435+
return timedEvent;
469436
}
470437

471-
public string fixWeaponName(DamageEvent evt, string dmgType = "Unknown") {
472-
string weaponName = "";
473-
if (evt.attacker.id is TimedExplosive) {
474-
weaponName = "Explosive Charge";
475-
} else if (evt.attacker.id is TimedGrenade) {
476-
weaponName = "F1 Grenade";
477-
} else if (evt.attacker.id.ToString().Contains("MutantBear")) {
478-
weaponName = "Mutant Bear Claw";
479-
} else if (evt.attacker.id.ToString().Contains("Bear")) {
480-
weaponName = "Bear Claw";
481-
} else if (evt.attacker.id.ToString().Contains("MutantWolf")) {
482-
weaponName = "Mutant Wolf Claw";
483-
} else if (evt.attacker.id.ToString().Contains("Wolf")) {
484-
weaponName = "Wolf Claw";
485-
} else if (evt.attacker.id.Equals(evt.victim.id)) {
486-
weaponName = dmgType;
487-
} else {
488-
weaponName = "Hunting Bow";
438+
public List<IPTimedEvent> GetParallelTimer(string name) {
439+
return (from timer in ParallelTimers
440+
where timer.Name == name
441+
select timer).ToList();
442+
}
443+
444+
public void KillParallelTimer(string name) {
445+
foreach (IPTimedEvent timer in GetParallelTimer(name)) {
446+
timer.Kill();
447+
ParallelTimers.Remove(timer);
489448
}
490-
return weaponName;
449+
}
450+
451+
#endregion
452+
453+
public Dictionary<string, object> CreateDict() {
454+
return new Dictionary<string, object>();
491455
}
492456
}
493457
}

IronPythonModule/IronPythonModule.csproj

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<RootNamespace>IronPythonModule</RootNamespace>
99
<AssemblyName>IronPythonModule</AssemblyName>
1010
<UseMSBuildEngine>False</UseMSBuildEngine>
11-
<ProductVersion>8.0.30703</ProductVersion>
12-
<SchemaVersion>2.0</SchemaVersion>
1311
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
1412
</PropertyGroup>
1513
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@@ -32,6 +30,9 @@
3230
</PropertyGroup>
3331
<ItemGroup>
3432
<Reference Include="System" />
33+
<Reference Include="IronPython.Deps">
34+
<HintPath>ref\IronPython.Deps.dll</HintPath>
35+
</Reference>
3536
<Reference Include="Assembly-CSharp">
3637
<HintPath>ref\Assembly-CSharp.dll</HintPath>
3738
</Reference>
@@ -41,9 +42,6 @@
4142
<Reference Include="Fougerite">
4243
<HintPath>ref\Fougerite.dll</HintPath>
4344
</Reference>
44-
<Reference Include="IronPython.Deps">
45-
<HintPath>ref\IronPython.Deps.dll</HintPath>
46-
</Reference>
4745
<Reference Include="uLink">
4846
<HintPath>ref\uLink.dll</HintPath>
4947
</Reference>
@@ -56,7 +54,6 @@
5654
<Compile Include="IPPlugin.cs" />
5755
<Compile Include="LookUp.cs" />
5856
<Compile Include="IPModule.cs" />
59-
<Compile Include="FougeriteEx\DestroyEvent.cs" />
6057
<Compile Include="FougeriteEx\IPTimedEvent.cs" />
6158
<Compile Include="FougeriteEx\Dump.cs" />
6259
</ItemGroup>

0 commit comments

Comments
 (0)