-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathScript.hx
More file actions
404 lines (358 loc) · 13 KB
/
Script.hx
File metadata and controls
404 lines (358 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package funkin.backend.scripting;
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
import flixel.util.FlxStringUtil;
import haxe.io.Path;
import lime.app.Application;
@:allow(funkin.backend.scripting.ScriptPack)
/**
* Class used for scripting.
* Use `Script.create` to create a script.
*/
class Script extends FlxBasic implements IFlxDestroyable {
/**
* Use "static var thing = true;" in hscript to use those!!
* are reset every mod switch so once you're done with them make sure to make them null!!
*/
public static var staticVariables:Map<String, Dynamic> = [];
/**
* Gets the default variables for a script.
*/
public static function getDefaultVariables(?script:Script):Map<String, Dynamic> {
return [
// Haxe related stuff
"Std" => Std,
"Math" => Math,
"Reflect" => Reflect,
"StringTools" => StringTools,
"Json" => haxe.Json,
"Xml" => Xml,
"Type" => Type,
"Date" => Date,
"Lambda" => Lambda,
#if sys "Sys" => Sys, #end
// OpenFL & Lime related stuff
"Assets" => openfl.utils.Assets,
"Application" => lime.app.Application,
"Main" => funkin.backend.system.Main,
"window" => lime.app.Application.current.window,
// Flixel related stuff
"state" => flixel.FlxG.state,
"FlxG" => flixel.FlxG,
"FlxSprite" => flixel.FlxSprite,
"FlxBasic" => flixel.FlxBasic,
"FlxCamera" => flixel.FlxCamera,
"FlxEase" => flixel.tweens.FlxEase,
"FlxTween" => flixel.tweens.FlxTween,
"FlxSound" => flixel.sound.FlxSound,
"FlxAssets" => flixel.system.FlxAssets,
"FlxMath" => flixel.math.FlxMath,
"FlxGroup" => flixel.group.FlxGroup,
"FlxTypedGroup" => flixel.group.FlxGroup.FlxTypedGroup,
"FlxSpriteGroup" => flixel.group.FlxSpriteGroup,
"FlxTypeText" => flixel.addons.text.FlxTypeText,
"FlxText" => flixel.text.FlxText,
"FlxTimer" => flixel.util.FlxTimer,
"FlxPoint" => CoolUtil.getMacroAbstractClass("flixel.math.FlxPoint"),
"FlxAxes" => CoolUtil.getMacroAbstractClass("flixel.util.FlxAxes"),
"FlxColor" => CoolUtil.getMacroAbstractClass("flixel.util.FlxColor"),
// Engine related stuff
"engine" => {
commit: Flags.COMMIT_NUMBER,
hash: Flags.COMMIT_HASH,
build: 2675, // 2675 being the last build num before it was removed
name: "Codename Engine"
},
"ModState" => funkin.backend.scripting.ModState,
"ModSubState" => funkin.backend.scripting.ModSubState,
"PlayState" => funkin.game.PlayState,
"GameOverSubstate" => funkin.game.GameOverSubstate,
"HealthIcon" => funkin.game.HealthIcon,
"HudCamera" => funkin.game.HudCamera,
"Note" => funkin.game.Note,
"Strum" => funkin.game.Strum,
"StrumLine" => funkin.game.StrumLine,
"Character" => funkin.game.Character,
"Boyfriend" => funkin.game.Character, // for compatibility
"PauseSubstate" => funkin.menus.PauseSubState,
"FreeplayState" => funkin.menus.FreeplayState,
"MainMenuState" => funkin.menus.MainMenuState,
"PauseSubState" => funkin.menus.PauseSubState,
"StoryMenuState" => funkin.menus.StoryMenuState,
"TitleState" => funkin.menus.TitleState,
"Options" => funkin.options.Options,
"Paths" => funkin.backend.assets.Paths,
"Conductor" => funkin.backend.system.Conductor,
"FunkinShader" => funkin.backend.shaders.FunkinShader,
"CustomShader" => funkin.backend.shaders.CustomShader,
"FunkinText" => funkin.backend.FunkinText,
"FlxAnimate" => funkin.backend.FlxAnimate,
"FunkinSprite" => funkin.backend.FunkinSprite,
"Alphabet" => funkin.menus.ui.Alphabet,
"Flags" => funkin.backend.system.Flags,
"CoolUtil" => funkin.backend.utils.CoolUtil,
"IniUtil" => funkin.backend.utils.IniUtil,
"XMLUtil" => funkin.backend.utils.XMLUtil,
#if sys "ZipUtil" => funkin.backend.utils.ZipUtil, #end
"MarkdownUtil" => funkin.backend.utils.MarkdownUtil,
"EngineUtil" => funkin.backend.utils.EngineUtil,
"MemoryUtil" => funkin.backend.utils.MemoryUtil,
"BitmapUtil" => funkin.backend.utils.BitmapUtil,
#if TRANSLATIONS_SUPPORT
"TranslationUtil" => funkin.backend.utils.TranslationUtil,
"translate" => funkin.backend.utils.TranslationUtil.get,
#end
];
}
/**
* Used internally to keep backwards compatibility with old scripts.
* This gets set on `hscript.Interp.importRedirects`,
* if you wanna modify it, please edit `hscript.Interp.importRedirects` directly.
**/
public static function getDefaultImportRedirects():Map<String, String> {
var redirects:Map<String, String> = [];
// Events
final events = "funkin.backend.scripting.events.";
redirects[events + "CharacterNodeEvent"] = events + "character.CharacterNodeEvent";
redirects[events + "CharacterXMLEvent"] = events + "character.CharacterXMLEvent";
redirects[events + "DanceEvent"] = events + "character.DanceEvent";
redirects[events + "DirectionAnimEvent"] = events + "character.DirectionAnimEvent";
redirects[events + "DiscordPresenceUpdateEvent"] = events + "discord.DiscordPresenceUpdateEvent";
redirects[events + "GameOverCreationEvent"] = events + "gameover.GameOverCreationEvent";
redirects[events + "CamMoveEvent"] = events + "gameplay.CamMoveEvent";
redirects[events + "CountdownEvent"] = events + "gameplay.CountdownEvent";
redirects[events + "EventGameEvent"] = events + "gameplay.EventGameEvent";
redirects[events + "GameOverEvent"] = events + "gameplay.GameOverEvent";
redirects[events + "RatingUpdateEvent"] = events + "gameplay.RatingUpdateEvent";
redirects[events + "HealthIconChangeEvent"] = events + "healthicon.HealthIconChangeEvent";
redirects[events + "FreeplayAlphaUpdateEvent"] = events + "menu.freeplay.FreeplayAlphaUpdateEvent";
redirects[events + "FreeplaySongSelectEvent"] = events + "menu.freeplay.FreeplaySongSelectEvent";
redirects[events + "MenuChangeEvent"] = events + "menu.MenuChangeEvent";
redirects[events + "PauseCreationEvent"] = events + "menu.pause.PauseCreationEvent";
redirects[events + "WeekSelectEvent"] = events + "menu.storymenu.WeekSelectEvent";
redirects[events + "InputSystemEvent"] = events + "note.InputSystemEvent";
redirects[events + "NoteCreationEvent"] = events + "note.NoteCreationEvent";
redirects[events + "NoteHitEvent"] = events + "note.NoteHitEvent";
redirects[events + "NoteMissEvent"] = events + "note.NoteMissEvent";
redirects[events + "NoteUpdateEvent"] = events + "note.NoteUpdateEvent";
redirects[events + "SimpleNoteEvent"] = events + "note.SimpleNoteEvent";
redirects[events + "StrumCreationEvent"] = events + "note.StrumCreationEvent";
redirects[events + "SplashShowEvent"] = events + "splash.SplashShowEvent";
redirects[events + "PlayAnimContext"] = events + "sprite.PlayAnimContext";
redirects[events + "PlayAnimEvent"] = events + "sprite.PlayAnimEvent";
redirects[events + "StageNodeEvent"] = events + "stage.StageNodeEvent";
redirects[events + "StageXMLEvent"] = events + "stage.StageXMLEvent";
// Old State Names
redirects["funkin.menus.BetaWarningState"] = "funkin.menus.WarningState";
return redirects;
}
/**
* Gets the default defines for a script.
* Includes all of the defines that the build was compiled with.
*/
public static function getDefaultPreprocessors():Map<String, Dynamic> {
var defines = funkin.backend.system.macros.DefinesMacro.defines;
defines.set("CODENAME_ENGINE", true);
defines.set("CODENAME_VER", Flags.VERSION);
defines.set("CODENAME_BUILD", 2675); // 2675 being the last build num before it was removed
defines.set("CODENAME_COMMIT", Flags.COMMIT_NUMBER);
return defines;
}
/**
* All available script extensions
*/
public static var scriptExtensions:Array<String> = [
"hx", "hscript", "hsc", "hxs",
"pack", // combined file
"lua" /** ACTUALLY NOT SUPPORTED, ONLY FOR THE MESSAGE **/
];
/**
* Currently executing script.
*/
public static var curScript:Script = null;
/**
* Script name (with extension)
*/
public var fileName:String;
/**
* Script Extension
*/
public var extension:String;
/**
* Path to the script.
*/
public var path:String = null;
private var rawPath:String = null;
private var didLoad:Bool = false;
/**
* Remapped filenames.
* Used for trace messages, to show what mod the script is from.
*/
public var remappedNames:Map<String, String> = [];
/**
* Creates a script from the specified asset path. The language is automatically determined.
* @param path Path in assets
*/
public static function create(path:String):Script {
if (Assets.exists(path)) {
return switch(Path.extension(path).toLowerCase()) {
case "hx" | "hscript" | "hsc" | "hxs":
new HScript(path);
case "pack":
var arr = Assets.getText(path).split("________PACKSEP________");
fromString(arr[1], arr[0]);
case "lua":
Logs.error("Lua is not supported in this engine. Use HScript instead.");
new DummyScript(path);
default:
new DummyScript(path);
}
}
return new DummyScript(path);
}
/**
* Creates a script from the string. The language is determined based on the path.
* @param code code
* @param path filename
*/
public static function fromString(code:String, path:String):Script {
return switch(Path.extension(path).toLowerCase()) {
case "hx" | "hscript" | "hsc" | "hxs":
new HScript(path).loadFromString(code);
case "lua":
Logs.error("Lua is not supported in this engine. Use HScript instead.");
new DummyScript(path).loadFromString(code);
default:
new DummyScript(path).loadFromString(code);
}
}
/**
* Creates a new instance of the script class.
* @param path
*/
public function new(path:String) {
super();
rawPath = path;
path = Paths.getFilenameFromLibFile(path);
fileName = Path.withoutDirectory(path);
extension = Path.extension(path);
this.path = path;
onCreate(path);
for(k=>e in getDefaultVariables(this)) {
set(k, e);
}
set("disableScript", () -> {
active = false;
});
set("__script__", this);
}
/**
* Loads the script
*/
public function load() {
if(didLoad) return;
var oldScript = curScript;
curScript = this;
onLoad();
curScript = oldScript;
didLoad = true;
}
/**
* HSCRIPT ONLY FOR NOW
* Sets the "public" variables map for ScriptPack
*/
public function setPublicMap(map:Map<String, Dynamic>) {
}
/**
* Hot-reloads the script, if possible
*/
public function reload() {
}
/**
* Traces something as this script.
*/
public function trace(v:Dynamic) {
var fileName = this.fileName;
if(remappedNames.exists(fileName))
fileName = remappedNames.get(fileName);
Logs.traceColored([
Logs.logText(fileName + ': ', GREEN),
Logs.logText(Std.string(v))
], TRACE);
}
/**
* Calls the function `func` defined in the script.
* @param func Name of the function
* @param parameters (Optional) Parameters of the function.
* @return Result (if void, then null)
*/
public function call(func:String, ?parameters:Array<Dynamic>):Dynamic {
var oldScript = curScript;
curScript = this;
var result = onCall(func, parameters == null ? [] : parameters);
curScript = oldScript;
return result;
}
/**
* Loads the code from a string, doesn't really work after the script has been loaded
* @param code The code.
*/
public function loadFromString(code:String) {
return this;
}
/**
* Sets a script's parent object so that its properties can be accessed easily. Ex: Passing `PlayState.instance` will allow `boyfriend` to be typed instead of `PlayState.instance.boyfriend`.
* @param variable Parent variable.
*/
public function setParent(variable:Dynamic) {}
/**
* Gets the variable `variable` from the script's variables.
* @param variable Name of the variable.
* @return Variable (or null if it doesn't exists)
*/
public function get(variable:String):Dynamic {return null;}
/**
* Sets the variable `variable` from the script's variables.
* @param variable Name of the variable.
* @return Variable (or null if it doesn't exists)
*/
public function set(variable:String, value:Dynamic):Void {}
/**
* Shows an error from this script.
* @param text Text of the error (ex: Null Object Reference).
* @param additionalInfo Additional information you could provide.
*/
public function error(text:String, ?additionalInfo:Dynamic):Void {
var fileName = this.fileName;
if(remappedNames.exists(fileName))
fileName = remappedNames.get(fileName);
Logs.traceColored([
Logs.logText(fileName, RED),
Logs.logText(text)
], ERROR);
}
override public function toString():String {
return FlxStringUtil.getDebugString(didLoad ? [
LabelValuePair.weak("path", path),
LabelValuePair.weak("active", active),
] : [
LabelValuePair.weak("path", path),
LabelValuePair.weak("active", active),
LabelValuePair.weak("loaded", didLoad),
]);
}
/**
* PRIVATE HANDLERS - DO NOT TOUCH
*/
private function onCall(func:String, parameters:Array<Dynamic>):Dynamic {
return null;
}
/**
* Called when the script is created.
* @param path Path to the script
*/
public function onCreate(path:String) {}
/**
* Called when the script is loaded.
*/
public function onLoad() {}
}