changhong/lua_probject/base_project/Main.lua

492 lines
15 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 'FairyGUI'
require 'Game.ControllerManager'
require 'Game.ViewManager'
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()
if true then
local suc = pcall(function()
local path = Application.streamingAssetsPath .. "/../../.."
package.cpath = package.cpath .. ";" .. path .. "/?.dll"
local dbg = require("emmy_core")
dbg.tcpConnect('localhost', 9966)
end)
if suc then
print("=============================调试连接成功!===========================")
end
end
-- 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 = "SIYUAN"
FairyGUI.FontManager.RegisterFont(FairyGUI.DynamicFont.New("SIYUAN", "base/static/fonts/SIYUAN.TTF"), null)
FairyGUI.FontManager.RegisterFont(
FairyGUI.DynamicFont.New("AlimamaFangYuanTiVF-Thin", "base/static/fonts/AlimamaFangYuanTiVF-Thin.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
debug_print = true
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")
TimerManager.New()
-- test:DynamicInvoke("222")
--ExtendManager.Init()
ControllerManager.Init()
ViewManager.Init()
ControllerManager.ChangeController(LoginController)
ViewManager.ChangeView(ViewManager.View_Login)
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 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 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
function TableToString(t, indent, visited)
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)
elseif type(t) == "function" then
-- 尝试获取函数名
local info = debug.getinfo(t, "n")
if info and info.name and info.name ~= "" then
return '"<function: ' .. info.name .. '>"'
else
return '"<function>"'
end
else
return '"<' .. type(t) .. '>"'
end
end
visited = visited or {}
if visited[t] then
return '"<cyclic reference>"'
end
visited[t] = true
indent = indent or ""
local nextIndent = indent .. " "
-- 更准确的数组检测
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
local result = ""
if isArray then
result = result .. "["
local parts = {}
for i = 1, maxIndex do
parts[i] = TableToString(t[i], nextIndent, visited)
end
result = result .. table.concat(parts, ",")
result = result .. "]"
else
result = result .. "{\n"
local parts = {}
local count = 0
for k, v in pairs(t) do
count = count + 1
local key
if type(k) == "string" and string.match(k, "^[a-zA-Z_][a-zA-Z0-9_]*$") then
key = '"' .. k .. '"' -- 即使是标识符也加双引号保持JSON格式
else
key = '"' .. tostring(k):gsub('["\\]', '\\%0') .. '"'
end
local valueStr
if type(v) == "function" then
-- 对函数进行特殊处理,只显示方法名
local info = debug.getinfo(v, "n")
if info and info.name and info.name ~= "" then
valueStr = '"<function: ' .. info.name .. '>"'
else
valueStr = '"<function>"'
end
else
valueStr = TableToString(v, nextIndent, visited)
end
parts[count] = nextIndent .. key .. ": " .. valueStr
end
-- 按键名排序,使输出更一致
table.sort(parts, function(a, b)
local keyA = a:match('^%s*"([^"]+)"')
local keyB = b:match('^%s*"([^"]+)"')
return (keyA or "") < (keyB or "")
end)
result = result .. table.concat(parts, ",\n")
if count > 0 then
result = result .. "\n" .. indent
end
result = result .. "}"
end
visited[t] = nil
return result
end