script_stubs.lua 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --[[
  2. Stubs for the functions that the decompiled world map scripts (wm0.lua, wm2.lua and wm3.lua) call
  3. and that are not implemented yet.
  4. Each one does nothing and returns 0, and is reported the first time it is called, so the scripts
  5. that use them keep running instead of failing with a Lua error. They are not an implementation:
  6. the names come from the decompiler, and most of the ones called "kernel_unknown_*" have an unknown
  7. purpose.
  8. A function that is already defined somewhere else is never replaced.
  9. ]]
  10. WorldMapStubs = WorldMapStubs or { reported = {} }
  11. --- Creates the stub of a function.
  12. -- @param name Name of the function.
  13. -- @return The stub.
  14. local function make_stub(name, quiet)
  15. return function(...)
  16. if not quiet and not WorldMapStubs.reported[name] then
  17. WorldMapStubs.reported[name] = true
  18. print("World map script function not implemented: " .. name)
  19. end
  20. return 0
  21. end
  22. end
  23. --- Bitwise and of two numbers. The scripts of the world map use it for flags, and the Lua of the
  24. -- game has no bit library.
  25. BitAnd = function(a, b)
  26. a = math.floor(a) % 4294967296
  27. b = math.floor(b) % 4294967296
  28. local result, bit = 0, 1
  29. for _ = 1, 32 do
  30. if a % 2 == 1 and b % 2 == 1 then result = result + bit end
  31. a = math.floor(a / 2)
  32. b = math.floor(b / 2)
  33. bit = bit * 2
  34. end
  35. return result
  36. end
  37. --- Bitwise or of two numbers.
  38. BitOr = function(a, b)
  39. a = math.floor(a) % 4294967296
  40. b = math.floor(b) % 4294967296
  41. local result, bit = 0, 1
  42. for _ = 1, 32 do
  43. if a % 2 == 1 or b % 2 == 1 then result = result + bit end
  44. a = math.floor(a / 2)
  45. b = math.floor(b / 2)
  46. bit = bit * 2
  47. end
  48. return result
  49. end
  50. -- Functions of the decompiled scripts with a known name.
  51. local stubbed_functions = {
  52. "Wait", "SetWaitFrames", "LoadModel", "SetActiveEntityDirectionAndFacing",
  53. "SetActiveEntityDirection", "SetActiveEntityMovespeed", "SetActiveEntityMeshCoordinates",
  54. "SetActiveEntityMeshCoordinatesInMesh", "SetLightMeshCoordinates",
  55. "SetLightMeshCoordinatesInMesh", "SetActivePoint", "WaitForWindowReady",
  56. "WaitForMessageAcknowledge", "SetWindowDimensions", "SetWindowMessage", "SetWindowParameters",
  57. "SetControlLock", "PlaySoundEffect", "EnterVehicle", "FadeOut", "FadeIn",
  58. "SetSubmarineColor", "SetChocoboType", "SetWorldProgress", "SetMeteorTexture", "Unknown333",
  59. "Unknown_AKAO", "SetBattleLock", "TriggerBattle", "UnknownFunction1B",
  60. }
  61. for _, name in ipairs(stubbed_functions) do
  62. if rawget(_G, name) == nil then
  63. -- The ones with a known name are only the things that the placement of the entities doesn't
  64. -- need (windows, waits, locks): not reported. The unknown ones are, to see what is missing.
  65. _G[name] = make_stub(name, true)
  66. end
  67. end
  68. -- The decompiler names the functions it can't identify "kernel_unknown_0x<opcode>". There are too
  69. -- many to list, and a new installation can have more, so they are stubbed when they are used.
  70. local previous_metatable = getmetatable(_G)
  71. setmetatable(_G, {
  72. __index = function(table, key)
  73. if type(key) == "string" and key:match("^kernel_unknown_0x%x+$") then
  74. local stub = make_stub(key)
  75. rawset(table, key, stub)
  76. return stub
  77. end
  78. if previous_metatable and previous_metatable.__index then
  79. if type(previous_metatable.__index) == "function" then
  80. return previous_metatable.__index(table, key)
  81. end
  82. return previous_metatable.__index[key]
  83. end
  84. return nil
  85. end,
  86. __newindex = previous_metatable and previous_metatable.__newindex or nil,
  87. })