com.amanotes.gdk 0.2.48 → 0.2.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,185 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Linq;
4
+ using System.Reflection;
5
+ using UnityEngine;
6
+
7
+ namespace Amanotes.Core
8
+ {
9
+ public partial class AmaGDK
10
+ {
11
+ internal static readonly Dispatcher dispatcher = new Dispatcher();
12
+
13
+ internal static void ThrowExceptionInEditor<T>(string message, Func<string, T> exceptionCreator) where T: Exception
14
+ {
15
+ #if UNITY_EDITOR
16
+ throw exceptionCreator(message);
17
+ #else
18
+ Debug.LogWarning(message);
19
+ #endif
20
+ }
21
+
22
+ [Serializable] internal class EventEntry
23
+ {
24
+ public int nParams;
25
+ internal bool _dispatching;
26
+ public Delegate callbacks;
27
+ public Delegate callbackOnce;
28
+
29
+ public bool hasCallback => callbacks != null || callbackOnce != null;
30
+
31
+ internal void Add(Delegate d, bool once)
32
+ {
33
+ callbackOnce = Delegate.Remove(callbackOnce, d);
34
+ callbacks = Delegate.Remove(callbacks, d);
35
+ if (once)
36
+ {
37
+ callbackOnce = Delegate.Combine(callbackOnce, d);
38
+ }
39
+ else
40
+ {
41
+ callbacks = Delegate.Combine(callbacks, d);
42
+ }
43
+ }
44
+
45
+ internal void Remove(Delegate d)
46
+ {
47
+ callbackOnce = Delegate.Remove(callbackOnce, d);
48
+ callbacks = Delegate.Remove(callbacks, d);
49
+ }
50
+
51
+
52
+
53
+ internal bool Dispatch(string eventName, object[] args)
54
+ {
55
+ if (_dispatching)
56
+ {
57
+ ThrowExceptionInEditor($"Nested dispatching is not supported, eventName: <{eventName}>",
58
+ (m) => new InvalidOperationException(m));
59
+ return false;
60
+ }
61
+
62
+ if (args.Length != nParams)
63
+ {
64
+ ThrowExceptionInEditor($"Dispatch <{eventName}> with incorrect number of params, expecting {nParams}, got {args.Length}!",
65
+ (m) => new TargetParameterCountException(m));
66
+ return false;
67
+ }
68
+
69
+ _dispatching = true;
70
+ callbacks?.DynamicInvoke(args);
71
+ callbackOnce?.DynamicInvoke(args);
72
+ callbackOnce = null;
73
+ _dispatching = false;
74
+
75
+ return true;
76
+ }
77
+ }
78
+
79
+ internal class Dispatcher
80
+ {
81
+ internal readonly Dictionary<string, EventEntry> _eventMap = new Dictionary<string, EventEntry>();
82
+ internal void ClearAndReset()
83
+ {
84
+ _eventMap.Clear();
85
+ }
86
+
87
+ internal EventEntry Get(string eventName)
88
+ {
89
+ if (string.IsNullOrEmpty(eventName)) return null;
90
+ _eventMap.TryGetValue(eventName, out EventEntry entry);
91
+ return entry;
92
+ }
93
+
94
+ internal void AddListener(string eventName, Delegate d, bool once)
95
+ {
96
+ if (d == null)
97
+ {
98
+ Debug.LogWarning("Dispatcher.Add() error: d == null");
99
+ return;
100
+ }
101
+
102
+ int nParams = d.Method.GetParameters().Length;
103
+ EventEntry entry = Get(eventName);
104
+
105
+ if (entry == null)
106
+ {
107
+ entry = new EventEntry { nParams = nParams };
108
+ _eventMap.Add(eventName, entry);
109
+ }
110
+ else if (nParams != entry.nParams)
111
+ {
112
+ ThrowExceptionInEditor(string.Format(Internal.Messsage.DISPATCHER_WRONG_NUMBER_OF_PARAMS, eventName, entry.nParams, nParams),
113
+ (m) => new TargetParameterCountException(m));
114
+ return;
115
+ }
116
+
117
+ // Debug.Log("Add: " + eventName + " --> " + d + " | " + once);
118
+ entry.Add(d, once);
119
+ }
120
+
121
+ internal void RemoveListener(string eventName, Delegate d)
122
+ {
123
+ if (d == null)
124
+ {
125
+ Debug.LogWarning("Dispatcher.Remove() error: d == null");
126
+ return;
127
+ }
128
+
129
+ EventEntry entry = Get(eventName);
130
+ entry?.Remove(d);
131
+ }
132
+
133
+ internal void ClearListeners(string eventName)
134
+ {
135
+ if (_eventMap.ContainsKey(eventName)) _eventMap.Remove(eventName);
136
+ }
137
+
138
+ internal void Dispatch(string eventName, params object[] data)
139
+ {
140
+ if (string.IsNullOrEmpty(eventName))
141
+ {
142
+ ThrowExceptionInEditor(
143
+ $"Invalid eventName: <{eventName}> (should not be null or empty)",
144
+ (m)=> new ArgumentException(m));
145
+ return;
146
+ }
147
+
148
+ EventEntry entry = Get(eventName);
149
+ if (entry == null) return;
150
+
151
+ if (!entry.hasCallback)
152
+ {
153
+ // string[] result = _eventMap
154
+ // .Select(kvp => kvp.Key + ":" + kvp.Value + "\n")
155
+ // .ToArray();
156
+ //
157
+ // Debug.LogWarning("AmaGDK [Event] No listener of type <" + eventName + ">\n\nRegistered events:\n\n" + string.Join("", result));
158
+ return;
159
+ }
160
+
161
+ TryCatchWrapper(() =>
162
+ {
163
+ entry.Dispatch(eventName, data);
164
+ }, !Application.isEditor);
165
+ }
166
+
167
+ internal static void TryCatchWrapper(Action action, bool useTryCatch)
168
+ {
169
+ if (!useTryCatch)
170
+ {
171
+ action();
172
+ return;
173
+ }
174
+
175
+ try
176
+ {
177
+ action();
178
+ } catch (Exception e)
179
+ {
180
+ Debug.LogWarning(e);
181
+ }
182
+ }
183
+ }
184
+ }
185
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: b03cd92a98d340e0afed1f2e620fbc2c
3
+ timeCreated: 1705375398
@@ -26,6 +26,8 @@ namespace Amanotes.Core.Internal
26
26
  public const string SDK_INIT_CALLED = "init has been called!";
27
27
  public const string MULTIPLE_INSTANCE = "multiple instance found!";
28
28
  public const string CONFIG_NOT_FOUND = "AmaGDKConfig not found in Resources!";
29
+ public const string DISPATCHER_WRONG_NUMBER_OF_PARAMS = "AddListener for eventName <{0}> expect {1} params but got {2}";
30
+
29
31
  }
30
32
 
31
33
  public static class Res
@@ -124,6 +126,7 @@ namespace Amanotes.Core.Internal
124
126
  // Reset all modules
125
127
  Ads.context = null;
126
128
  Ads._adapter = null;
129
+ dispatcher.ClearAndReset();
127
130
 
128
131
  _status = Status.None;
129
132
  _allowInit = false;
@@ -66,6 +66,56 @@ namespace Amanotes.Core.Internal
66
66
  if (Time.realtimeSinceStartup - time > timeoutInSecs) yield break;
67
67
  }
68
68
  }
69
+
70
+
71
+ public static bool IsFromTemplate(string formattedString, string template)
72
+ {
73
+ // Split the original template into segments based on the positions of placeholders
74
+ string[] segments = SplitTemplate(template);
75
+
76
+ // Check if each segment exists in the output string in the correct order
77
+ return AreSegmentsPresentInOrder(segments, formattedString);
78
+ }
79
+
80
+ static bool AreSegmentsPresentInOrder(string[] segments, string outputString)
81
+ {
82
+ var lastIndex = 0;
83
+
84
+ // Check if each segment exists in the output string in the correct order
85
+ foreach (string segment in segments)
86
+ {
87
+ int index = outputString.IndexOf(segment, lastIndex, StringComparison.Ordinal);
88
+
89
+ if (index == -1) return false;
90
+ lastIndex = index + segment.Length;
91
+ }
92
+
93
+ return true;
94
+ }
95
+
96
+ static string[] SplitTemplate(string template)
97
+ {
98
+ // Create a regex pattern to match placeholders like {0}, {1}, {2}, etc.
99
+ var pattern = @"\{(\d+)\}";
100
+
101
+ // Extract the indexes of placeholders from the original template
102
+ MatchCollection matches = Regex.Matches(template, pattern);
103
+
104
+ // Split the original template into segments based on the matched indexes
105
+ var segments = new string[matches.Count + 1];
106
+ var lastIndex = 0;
107
+
108
+ foreach (Match match in matches)
109
+ {
110
+ int index = int.Parse(match.Groups[1].Value);
111
+ segments[index] = template.Substring(lastIndex, match.Index - lastIndex);
112
+ lastIndex = match.Index + match.Length;
113
+ }
114
+
115
+ // The last segment corresponds to the content after the last placeholder
116
+ segments[matches.Count] = template.Substring(lastIndex);
117
+ return segments;
118
+ }
69
119
  }
70
120
 
71
121
  public static class GDKReflection
@@ -87,8 +137,8 @@ namespace Amanotes.Core.Internal
87
137
  }
88
138
 
89
139
  }
90
-
91
- internal static class GDKFileUtils
140
+
141
+ public static class GDKFileUtils
92
142
  {
93
143
  private static string _basePath;
94
144
  private static string basePath
@@ -115,7 +165,7 @@ namespace Amanotes.Core.Internal
115
165
  return File.Exists(GetPath(fileName));
116
166
  }
117
167
 
118
- internal static void Save(string fileName, string content)
168
+ public static void Save(string fileName, string content)
119
169
  {
120
170
  if (string.IsNullOrWhiteSpace(fileName))
121
171
  {
@@ -188,7 +238,7 @@ namespace Amanotes.Core.Internal
188
238
  }
189
239
  }
190
240
 
191
- internal static string Load(string fileName)
241
+ public static string Load(string fileName)
192
242
  {
193
243
  var content = "";
194
244
 
@@ -247,6 +297,33 @@ namespace Amanotes.Core.Internal
247
297
  }
248
298
  Debug.Log(sb);
249
299
  }
300
+
301
+ public static T LoadJsonFromFile<T>(T instance) where T: new()
302
+ {
303
+ instance ??= new T();
304
+
305
+ string fileName = instance.GetType().Name;
306
+ string json = Load(fileName);
307
+ if (!string.IsNullOrEmpty(json))
308
+ {
309
+ try
310
+ {
311
+ JsonUtility.FromJsonOverwrite(json, instance);
312
+ } catch (Exception e)
313
+ {
314
+ LogWarning("LoadJsonFromFile error: " + fileName + "\n" + e);
315
+ }
316
+ }
317
+
318
+ return instance;
319
+ }
320
+
321
+ public static void SaveJsonToFile<T>(T instance)
322
+ {
323
+ string fileName = instance.GetType().Name;
324
+ string json = JsonUtility.ToJson(instance);
325
+ Save(fileName, json);
326
+ }
250
327
  }
251
328
 
252
329
  public class ActionQueue
@@ -313,34 +390,18 @@ namespace Amanotes.Core.Internal
313
390
  }
314
391
 
315
392
  internal interface IJsonFile { }
393
+
316
394
 
317
395
  internal static class JsonFileExtension
318
396
  {
319
397
  public static T LoadJsonFromFile<T>(this T instance) where T : IJsonFile, new()
320
398
  {
321
- instance ??= new T();
322
-
323
- string fileName = instance.GetType().Name;
324
- string json = GDKFileUtils.Load(fileName);
325
- if (!string.IsNullOrEmpty(json))
326
- {
327
- try
328
- {
329
- JsonUtility.FromJsonOverwrite(json, instance);
330
- } catch (Exception e)
331
- {
332
- LogWarning("LoadJsonFromFile error: " + fileName + "\n" + e);
333
- }
334
- }
335
-
336
- return instance;
399
+ return GDKFileUtils.LoadJsonFromFile(instance);
337
400
  }
338
401
 
339
- public static void SaveJsonToFile<T>(this T instance) where T : IJsonFile
402
+ public static void SaveJsonToFile<T>(this T instance) where T: IJsonFile
340
403
  {
341
- string fileName = instance.GetType().Name;
342
- string json = JsonUtility.ToJson(instance);
343
- GDKFileUtils.Save(fileName, json);
404
+ GDKFileUtils.SaveJsonToFile(instance);
344
405
  }
345
406
  }
346
407
 
@@ -1,9 +1,5 @@
1
1
  fileFormatVersion: 2
2
- <<<<<<<< HEAD:GDK/AmaGDKCore/Runtime/Klavar/Attributes.meta
3
2
  guid: 74f302749097e44ff889ed117ec82dd3
4
- ========
5
- guid: 2fdae68db26424f679091198f471cf00
6
- >>>>>>>> fafdc57 ([Release] 0.2.47):GDKTest/GDK-Unity2020.3/Assets/AmaGDKTest.meta
7
3
  folderAsset: yes
8
4
  DefaultImporter:
9
5
  externalObjects: {}
@@ -1,9 +1,5 @@
1
1
  fileFormatVersion: 2
2
- <<<<<<<< HEAD:GDK/AmaGDKCore/Runtime/Klavar.meta
3
2
  guid: 5b981038cfa6f4ec2a7dd96f58fde6cd
4
- ========
5
- guid: 74f302749097e44ff889ed117ec82dd3
6
- >>>>>>>> fafdc57 ([Release] 0.2.47):GDK/AmaGDKCore/Runtime/Klavar/Attributes.meta
7
3
  folderAsset: yes
8
4
  DefaultImporter:
9
5
  externalObjects: {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.amanotes.gdk",
3
- "version": "0.2.48",
3
+ "version": "0.2.49",
4
4
  "displayName": "AmaGDK",
5
5
  "description": "Amanotes Game Development Kit",
6
6
  "unity": "2019.4",