LuaAsset.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Security.Cryptography;
  3. using UnityEngine;
  4. [Serializable]
  5. public class LuaAsset : ScriptableObject
  6. {
  7. public static string LuaDecodeKey = "LuaDecodeKey"; //TODO: use a safe method to hide decode key
  8. public static string[] LuaSearchingPaths = new []{
  9. "lua/",
  10. "lua/utility/",
  11. };
  12. public bool encode = true;
  13. public byte[] data;
  14. public byte[] GetDecodeBytes()
  15. {
  16. byte[] decode = this.data;
  17. // TODO: your decode function
  18. decode = encode ? Security.XXTEA.Decrypt(this.data, LuaAsset.LuaDecodeKey) : this.data;
  19. return data;
  20. }
  21. public static byte[] Require(ref string luapath)
  22. {
  23. return Require(luapath);
  24. }
  25. public static byte[] Require(string luapath, string search = "", int retry = 0)
  26. {
  27. if(string.IsNullOrEmpty(luapath))
  28. return null;
  29. var LuaExtension = ".lua";
  30. if(luapath.EndsWith(LuaExtension))
  31. {
  32. luapath = luapath.Remove(luapath.LastIndexOf(LuaExtension));
  33. }
  34. byte[] bytes = null;
  35. var assetName = search + luapath.Replace(".", "/") + LuaExtension;
  36. {
  37. //TODO: your bundle load method
  38. // var asset = AssetSys.GetAssetSync<LuaAsset>(assetName);
  39. var asset = Resources.Load<LuaAsset>(assetName);
  40. if (asset != null)
  41. {
  42. bytes = asset.GetDecodeBytes();
  43. }
  44. }
  45. // try next searching path
  46. if(bytes == null && retry < LuaSearchingPaths.Length)
  47. {
  48. bytes = Require(luapath, LuaSearchingPaths[retry], 1+retry);
  49. }
  50. Debug.Assert(bytes != null, $"{luapath} not found.");
  51. return bytes;
  52. }
  53. }