PreInit.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. --========================================================================
  2. -- PreInit.lua : Script that's run when the scripting system is initialized
  3. --
  4. -- Part of the GameCode4 Application
  5. --
  6. -- GameCode4 is the sample application that encapsulates much of the source code
  7. -- discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. -- "Rez" Graham, published by Charles River Media.
  9. -- ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. --
  11. -- If this source code has found it's way to you, and you think it has helped you
  12. -- in any way, do the authors a favor and buy a new copy of the book - there are
  13. -- detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. -- by clicking here:
  15. -- http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. --
  17. -- There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. --
  19. -- The source code is managed and maintained through Google Code:
  20. -- http://code.google.com/p/gamecode4/
  21. --
  22. -- (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. --
  24. -- This program is free software; you can redistribute it and/or
  25. -- modify it under the terms of the GNU Lesser GPL v3
  26. -- as published by the Free Software Foundation.
  27. --
  28. -- This program is distributed in the hope that it will be useful,
  29. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. -- http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. --
  33. -- You should have received a copy of the GNU Lesser GPL v3
  34. -- along with this program; if not, write to the Free Software
  35. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. --
  37. --========================================================================
  38. -- This file is run immediately after Lua is created but before the rest of the game initializes. This is the place
  39. -- to put any language extentions or pure utility functions/classes you write.
  40. -----------------------------------------------------------------------------------------------------------------------
  41. -- Redifinition of require() and dofile(). This is needed to allow scripts to go through the resource system.
  42. -----------------------------------------------------------------------------------------------------------------------
  43. do
  44. local OldRequire = require; -- save the old require() function
  45. local resourceIdMap = {}; -- map of resource id's we've already loaded
  46. require = function(script)
  47. -- If we haven't already loaded this file, try to load it now
  48. if (not resourceIdMap[script]) then
  49. if (LoadAndExecuteScriptResource(script)) then
  50. resourceIdMap[script] = true;
  51. else
  52. -- failed to load file through the resource system so fall back to the old method
  53. OldRequire(script)
  54. end
  55. end
  56. end
  57. end
  58. do
  59. local OldPrint = print;
  60. print = function(text)
  61. if (text ~= nil) then
  62. Log(text);
  63. else
  64. Log("<nil>");
  65. end
  66. end
  67. end
  68. -----------------------------------------------------------------------------------------------------------------------
  69. -- Utility functions
  70. -----------------------------------------------------------------------------------------------------------------------
  71. Utils = {};
  72. function Utils.DumpTable(table)
  73. for key, val in pairs(table) do
  74. print(key, val);
  75. end
  76. end
  77. -- private function used by Utils.DumpObject()
  78. function Utils._BuildMemberSet(obj, memberSet)
  79. if (obj.__index ~= nil and obj ~= obj.__index) then
  80. Utils._BuildMemberSet(obj.__index, memberSet);
  81. end
  82. for key, val in pairs(obj) do
  83. if (key ~= "_index" and memberSet[key] == nil) then
  84. memberSet[key] = true;
  85. end
  86. end
  87. end
  88. function Utils.DumpObject(obj)
  89. -- There might be copies of the same variable at various levels in the hierarchy so we need to build an
  90. -- exclusive set of names.
  91. local memberSet = {};
  92. Utils._BuildMemberSet(obj, memberSet);
  93. -- Loop through the set of member names and print them out, along with the value from the original object's
  94. -- perspective.
  95. for key,val in pairs(memberSet) do
  96. print(key, obj[key]);
  97. end
  98. end
  99. -----------------------------------------------------------------------------------------------------------------------
  100. -- Classes & Inheritance - Chapter 12, page 349
  101. -----------------------------------------------------------------------------------------------------------------------
  102. function class(baseClass, body)
  103. local ret = body or {};
  104. -- if there's a base class, attach our new class to it
  105. if (baseClass ~= nil) then
  106. setmetatable(ret, ret);
  107. ret.__index = baseClass;
  108. ret.base = baseClass;
  109. end
  110. -- Add the Create() function. This will end up being added to each subclass which isn't ideal. This
  111. -- function should probably be stored in a single place so we only have to create it once.
  112. -- -self: The self pointer that's calling this function.
  113. -- -constructionData: The table sent in as construction data. Think of it like a constructor.
  114. -- -originalSubClass: Used for recursion. Don't pass anything into this parameter, it's filled
  115. -- automatically. It's used in case the C++ side needs access to the leaf
  116. -- subclass that is being instantiated. For an example, see ScriptProcess
  117. -- in C++.
  118. ret.Create = function(self, constructionData, originalSubClass)
  119. local obj;
  120. if (self.__index ~= nil) then
  121. if (originalSubClass ~= nil) then
  122. obj = self.__index:Create(constructionData, originalSubClass);
  123. else
  124. obj = self.__index:Create(constructionData, self);
  125. end
  126. else
  127. obj = constructionData or {};
  128. end
  129. setmetatable(obj, obj);
  130. obj.__index = self;
  131. -- copy any operators over
  132. if (self.__operators ~= nil) then
  133. for key, val in pairs(self.__operators) do
  134. obj[key] = val;
  135. end
  136. end
  137. return obj;
  138. end
  139. -- Returns true if otherClass appears in this objects class hierarchy anywhere.
  140. ret.IsInstance = function(self, otherClass)
  141. local cls = self.__index;
  142. while cls do
  143. if cls == otherClass then
  144. return true
  145. end
  146. cls = cls.base
  147. end
  148. return false
  149. end
  150. return ret;
  151. end