dezhou_client/lua_probject/base_project/Main.lua

482 lines
15 KiB
Lua
Raw Normal View History

2025-12-17 21:08:27 +08:00
local breakSocketHandle, debugXpCall = require("LuaDebugjit")("localhost", 7003)
local timer = Timer.New(function()
breakSocketHandle()
end, 1, -1, false)
timer:Start();
require "Core.init"
json = require 'cjson'
require 'Game.ControllerManager'
-- require 'Game.ViewManager'
require('Game.UIManager')
require('Game.View.Common.EventManager')
require('Game.View.Common.LanguageManager')
require 'Game.DataManager'
require "Game.ExtendManager"
require "Game.ExtendHotupdate"
require "TableData"
MsgParser = require("MsgParser")
Utils = Game.Utils
PlayerPrefs = UnityEngine.PlayerPrefs
RuntimePlatform = UnityEngine.RuntimePlatform
Application = UnityEngine.Application
Screen = UnityEngine.Screen
ResourcesManager = taurus.unity.ResourcesManager
-- require 'tolua.reflection'
-- tolua.loadassembly('Assembly-CSharp')
-- local BindingFlags = require 'System.Reflection.BindingFlags'
local _game_info
local panel = nil
oldGameVersion = 2 --1 原始 2 老游戏新加功能
--主入口函数。从这里开始lua逻辑
function Main()
--PlayerPrefs.DeleteKey('session_id')
Application.targetFrameRate = 60
-- FairyGUI.UIConfig.buttonSound =FairyGUI.NAudioClip(ResourcesManager.LoadObject("base/common/sound/click.mp3",typeof(UnityEngine.AudioClip)))
-- FairyGUI.UIConfig.defaultFont = "FZDaBiaoSong-B06S"
-- FairyGUI.FontManager.RegisterFont(FairyGUI.DynamicFont.New("SIYUAN","base/static/fonts/SIYUAN.TTF"),null)
--FairyGUI.FontManager.RegisterFont(FairyGUI.DynamicFont.New("FZCuYuan-M03","base/static/fonts/FZCuYuan-M03.TTF"),null)
--FairyGUI.FontManager.RegisterFont(FairyGUI.DynamicFont.New("HYFangLiJ","base/static/fonts/HYFangLiJ.ttf"),null)
_game_info = json.decode(GameApplication.Instance.GameInfo)
--_game_info["login_url"]="http://8.134.59.224:8101/"
--pt(_game_info)
debug_print = false --GetGameInfo("debug_print")
if Application.platform == RuntimePlatform.WindowsEditor then
debug_print = true
end
local NetManager = taurus.client.NetManager
NetManager.debug_print = debug_print
-- 网络延时8秒
NetManager.TIMEOUT_TIME = 10
-- UIPackage.AddPackage("base/common/ui/Common")
-- panel = UIPackage.CreateObjectFromURL("ui://Common/UIPanel")
-- GRoot.inst:AddChildAt(panel, 0)
-- panel:MakeFullScreen()
-- panel:AddRelation(GRoot.inst, RelationType.Size)
--web网络API版本号
NetManager.VERSION = GetGameInfo("net_version")
UGUIRootCanvas = UnityEngine.GameObject.Find("Canvas")
LuaUIHelper = UGUIRootCanvas:GetComponent("LuaUIHelper")
CommonUISprite = LoadPrefabData("base/prefab/CommonUISprite.prefab", "base/prefab/CommonUISprite")
CommonUISprite = UnityEngine.GameObject.Instantiate(CommonUISprite)
CommonUISprite = CommonUISprite:GetComponent("LuaUIHelperSprite")
GameCardSprite = LoadPrefabData("base/prefab/GameCardSprite.prefab", "base/prefab/GameCardSprite")
GameCardSprite = UnityEngine.GameObject.Instantiate(GameCardSprite)
GameCardSprite = GameCardSprite:GetComponent("LuaUIHelperSprite")
GameUIText = LoadPrefabData("base/prefab/GameUIText.prefab", "base/prefab/GameUIText")
GameUIText = UnityEngine.GameObject.Instantiate(GameUIText)
GameUIText = GameUIText:GetComponent("LuaUIHelperText")
EventManager:Init()
TimerManager.New()
-- test:DynamicInvoke("222")
--ExtendManager.Init()
ControllerManager.Init()
-- ViewManager.Init()
UIManager.Init()
LanguageManager.Init()
ControllerManager.ChangeController(LoginController)
-- ViewManager.ChangeView(ViewManager.View_LoginNew)
UIManager.ShowUI(UIManager.ViewLogin)
DataManager.AppVersion = GetGameInfo("app_version")
get_gps()
local timer = 0
local DSTweenManager = ds.tween.DSTweenManager
UpdateBeat:Add(function()
local deltaTime = Time.deltaTime
DSTweenManager.Update(deltaTime)
timer = timer + deltaTime
if timer >= 60 then
timer = 0
local ctr = ControllerManager.GetCurrenController()
if DataManager.CurrenRoom and ctr.baseType == GameController then
ctr:GetGPS()
else
get_gps()
end
end
end)
end
function LoadPrefabData(url1, url2)
return ResourcesManager.LoadObjectByGroup(url1, typeof(UnityEngine.GameObject), url2)
end
function GetGameInfo(key)
return _game_info[key]
end
function GetPlatform()
return ResourcesManager.OS_Dir
end
function GetGameInfoPlatform(key)
local p_key = GetPlatform()
local _platfrom = _game_info[p_key]
return _platfrom[key]
end
function BlurView(view, enabled)
if enabled then
-- local bf = FairyGUI.BlurFilter()
-- bf.blurSize = 0.05
-- view.filter = bf
else
view.filter = null
end
end
function ShareScreenShotWithOption(callback, index)
--分享目标选择,包括分享截图到 微信/支付宝
local wx_win = BaseWindow.new("ui://Common/Win_WXShareWin", nil)
wx_win._close_destroy = true
wx_win._view:GetController("c1").selectedIndex = index or 3
local cb = function()
if callback then callback() end
end
wx_win._view:GetChild("btn_wx_session").onClick:Add(function()
wx_win:Destroy()
ShareScreenShot(1, callback)
end)
wx_win:Show()
end
function ShareScreenShot(n, callback)
local json_data = {}
json_data["title"] = "湘北联赛"
local mediaObject = {}
mediaObject["path"] = Application.persistentDataPath
mediaObject["filename"] = "screenshot"
mediaObject["type"] = 1
json_data["mediaObject"] = mediaObject
json_data["description"] = "一款现实中朋友约局休闲娱乐的场所!速度约朋友一起来玩吧!"
json_data["scene"] = 0
local json_str = json.encode(json_data)
TakeScreenShot.Take(function()
-- 1微信 2支付宝
GameApplication.Instance:ShareLink(n or 1, json_str, nil)
if callback then
callback()
end
end)
end
function shareQRCodePicture(url, secene)
--print(debug.traceback())
print(url)
print(secene)
local json_data = {}
json_data["title"] = "湘北联赛"
local mediaObject = {}
local filename = "qrcode" .. DataManager.SelfUser.account_id
print(Application.persistentDataPath)
mediaObject["path"] = Application.persistentDataPath
mediaObject["filename"] = filename
mediaObject["type"] = 1
json_data["mediaObject"] = mediaObject
json_data["description"] = "一款现实中朋友约局休闲娱乐的场所!速度约朋友一起来玩吧!"
json_data["scene"] = secene
print("json_data==================")
local json_str = json.encode(json_data)
pt(json_str)
local tex2 = QRCodePicture.GenerateQRcode(url, 250, 250)
local tex1 = ResourcesManager.LoadObject("base/lobby/bg/bg.png", typeof(UnityEngine.Texture2D))
filename = filename .. ".jpg"
print("text2==========")
print(tex2)
print("text1==========")
print(tex1)
print("filename==========")
print(filename)
QRCodePicture.CombanitePicture(tex1, tex2, 393, 1334 - 802 - 250, filename)
GameApplication.Instance:ShareLink(1, json_str, nil)
end
function ShareChatRoom(room_id, share_time, round, game_name, group_id, player_list, _root_view, play_name)
end
function UISetController(root, controller_name, gear_display, selectedIndex)
local ctr = root:GetController(controller_name)
local gear = gear_display:GetGear(0)
gear.controller = ctr
gear.pages = { ctr:GetPageId(selectedIndex) }
gear_display:HandleControllerChanged(ctr)
end
local bg_url = nil
function LoadGameBg(url, main_view)
-- local win_mode = main_view:GetChild("win_mode")
-- win_mode:RemoveChildren(0, -1, true)
-- local tex_bg = ResourcesManager.LoadObjectByGroup(url .. ".png", typeof(UnityEngine.Texture), url)
-- local bg = GImage()
-- bg.texture = FairyGUI.NTexture(tex_bg)
-- bg.width = win_mode.width
-- bg.height = win_mode.height
-- bg:AddRelation(win_mode, RelationType.Size)
-- win_mode:AddChild(bg)
-- -- 卸载资源
-- if url ~= bg_url and bg_url then
-- ResourcesManager.UnLoadGroup(bg_url)
-- end
-- bg_url = url
end
function AddPanel(child)
child:MakeFullScreen()
child:AddRelation(GRoot.inst, RelationType.Size)
panel:AddChild(child)
end
function AddPanelAt(child, index)
child:MakeFullScreen()
child:AddRelation(GRoot.inst, RelationType.Size)
panel:AddChildAt(child, index)
end
function AddPanelCenter(child)
panel:AddChild(child)
child:Center(true)
end
--重启游戏
function RestartGame()
DSTweenManager.ClearTween()
-- panel:Dispose()
coroutine.stopAll()
BaseWindow.DestroyAll()
GameApplication.Instance:RestartGame()
end
--场景切换通知
function OnLevelWasLoaded(level)
Time.timeSinceLevelLoad = 0
end
--程序切到后台
function OnApplicationPause()
-- ViewUtil.CloseModalWait()
ViewManager.OnApplicationPause()
end
--程序从后台切回
function OnApplicationActive()
ViewManager.OnApplicationActive()
end
function pt(...)
if debug_print then
local arg = { ... }
local has = false
for _, v in pairs(arg) do
if v and type(v) == "table" then
has = true
break
end
end
if not has then
print(...)
end
local content = ""
for _, v in pairs(arg) do
if v == "table" then
content = content .. tostring(v) .. "\n"
else
content = content .. "==>[T]:" .. LuaPrint(v, limit), debug.traceback() .. "\n"
end
print(content)
end
end
end
function TableToString(t)
if type(t) ~= "table" then
if type(t) == "string" then
return '"' .. t:gsub('["\\]', '\\%0') .. '"'
elseif type(t) == "number" or type(t) == "boolean" then
return tostring(t)
else
return "null"
end
end
-- 更准确的数组检测
local isArray = true
local maxIndex = 0
for k in pairs(t) do
if type(k) ~= "number" or k < 1 or k ~= math.floor(k) then
isArray = false
break
end
if k > maxIndex then
maxIndex = k
end
end
-- 检查连续性
if isArray and maxIndex > 0 then
for i = 1, maxIndex do
if t[i] == nil then
isArray = false
break
end
end
end
if isArray then
local parts = {}
for i = 1, maxIndex do
parts[i] = TableToString(t[i])
end
return "[" .. table.concat(parts, ",") .. "]"
else
local parts = {}
for k, v in pairs(t) do
local key = '"' .. tostring(k):gsub('["\\]', '\\%0') .. '"'
table.insert(parts, key .. ":" .. TableToString(v))
end
return "{" .. table.concat(parts, ",") .. "}"
end
end
function serialize(tbl, indent, visited)
indent = indent or ""
visited = visited or {}
-- 防止循环引用
if visited[tbl] then
return indent .. '"循环引用"'
end
visited[tbl] = true
local result = "{\n"
local newIndent = indent .. " "
for k, v in pairs(tbl) do
result = result .. newIndent
-- 处理键
if type(k) == "string" and string.match(k, "^[%a_][%a%d_]*$") then
result = result .. k .. " = "
else
result = result .. "[" .. tostring(k) .. "] = "
end
-- 处理值
if type(v) == "table" then
result = result .. serialize(v, newIndent, visited)
elseif type(v) == "string" then
result = result .. string.format("%q", v) -- 带引号的字符串
elseif type(v) == "function" then
result = result .. '"<function>"'
elseif type(v) == "userdata" then
result = result .. '"<userdata>"'
elseif type(v) == "thread" then
result = result .. '"<coroutine>"'
else
result = result .. tostring(v)
end
result = result .. ",\n"
end
return result .. indent .. "}"
end
function LuaPrint(lua_table, limit, indent, step)
step = step or 0
indent = indent or 0
local content = ""
if limit ~= nil then
if step > limit then
return "..."
end
end
if step > 10 then
return content .. "..."
end
if lua_table == nil then
return "nil"
end
if type(lua_table) == "userdata" or type(lua_table) == "lightuserdata" or type(lua_table) == "thread" then
return tostring(lua_table)
end
if type(lua_table) == "string" or type(lua_table) == "number" then
return "[No-Table]:" .. lua_table
end
for k, v in pairs(lua_table) do
if k ~= "_class_type" then
local szBuffer = ""
Typev = type(v)
if Typev == "table" then
szBuffer = "{"
end
local szPrefix = string.rep(" ", indent)
if Typev == "table" and v._fields then
local kk, vv = next(v._fields)
if type(vv) == "table" then
content = content .. "\n\t" .. kk.name .. "={" .. LuaPrint(vv._fields, 5, indent + 1, step + 1) ..
"}"
else
content = content .. "\n\t" .. kk.name .. "=" .. vv
end
else
if type(k) == "table" then
if k.name then
if type(v) ~= "table" then
content = content .. "\n" .. k.name .. "=" .. v
else
content = content .. "\n" .. k.name .. " = list:"
local tmp = "\n"
for ka, va in ipairs(v) do
tmp = tmp .. "#" .. ka .. "_" .. tostring(va)
end
content = content .. tmp
end
end
elseif type(k) == "function" then
content = content .. "\n fun=function"
else
formatting = szPrefix .. tostring(k) .. " = " .. szBuffer
if Typev == "table" then
content = content .. "\n" .. formatting
content = content .. LuaPrint(v, limit, indent + 1, step + 1)
content = content .. "\n" .. szPrefix .. "},"
else
local szValue = ""
if Typev == "string" then
szValue = string.format("%q", v)
else
szValue = tostring(v)
end
content = content .. "\n" .. formatting .. (szValue or "nil") .. ","
end
end
end
end
end
return content
end
function printlog(...)
if debug_print then
print(...)
end
end