/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018-03-12
 * 时间: 16:24
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using zyxAdapter;
using MrWu.Debug;


namespace nativeCom {


    public class dcEvent_zyx0 {

        public virtual void init() {
            log("zyxevent init...");
            //	dg_zyxfun dg=new dg_zyxfun( doMsg_fun);

        }
        public void log(string str, bool isError = false) {
            if (isError)
                MrWu.Debug.Debug.Error(str);
            else
                MrWu.Debug.Debug.Log(str);

        }

        public virtual myGameClassBase newobj(ref tr_rltpchar t) { return null; }

        /// <summary>
        /// c# dll过程入口
        /// </summary>
        /// <param name="dowhat"></param>
        /// <param name="datas"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        unsafe public virtual int doMsg_rukou(string dowhat/*^分割*/, string datas/*结构体地址 ^分割*/, ref tr_rltpchar t) {
            //			Thread.Sleep(10000);
            //			return 1;
            myGameClassBase mc = null;
            string[] ss = datas.Split('*');//
                                           //ss[0] myclass
            switch (ss[1]) {
                case "dotimer":
                case "init_deskmem":
                    break;
                default: log(dowhat + "  " + datas); break;
            }


            bool initedmem = true;
            switch (ss[1]) {
                case "init_deskmem":
                    if (ss[2] == "0")
                        initedmem = false;
                    break;
            }

            if (initedmem)
                mc = myGameObj.getObj(ss[0]) as myGameClassBase;
            else {
                mc = newobj(ref t);

            }
            Igamedo ido = mc as Igamedo;
            if (ido != null) {

                if (!initedmem)
                    ido.i_do_initMem(false);

                switch (ss[1]) {
                    case "init_deskmem":
                        if (ss[2] == "1") {
                            ido.i_do_initMem(true);
                            mc.releaseAddr();//!!!!必须
                        } else {

                        }
                        //					mc.do_initmem(true);
                        break;
                    case "startwar":
                        ido.i_do_startWar();
                        break;
                    case "reconnect"://ss[2] 谁id重连
                        ido.i_do_reconect();
                        break;
                    case "dopack":



                        int len = int.Parse(ss[3]);
                        byte[] data = new byte[len];
                        IntPtr ptr = new IntPtr(int.Parse(ss[2]));


                        Marshal.Copy(ptr, data, 0, len);

                        int evt;
                        object obj = cls_nativeCom.Instance.DeSerizeable_obj_withEvt(data, out evt);

                        ido.i_do_pack(obj, evt);

                        //test发包回去

                        //mc.sendpack(obj,84524);

                        data = null;
                        break;
                    case "dotimer":

                        ido.i_do_timer();
                        break;
                    case "doOthers":
                        switch (ss[2]) {
                            case "setdata_dapai":
                                len = int.Parse(ss[6]);

                                ptr = new IntPtr(int.Parse(ss[5]));


                                //								data=new byte[len];
                                //								Marshal.Copy(ptr,data,0,len);
                                //									ido.i_getData(data);
                                //
                                //									break;
                                string str = Marshal.PtrToStringAnsi(ptr);
                                ido.i_getData(str);
                                //								IntPtr init = Marshal.StringToHGlobalAnsi(textBox1.Text);
                                //
                                //textBox2.Text = Marshal.PtrToStringAnsi(outit);
                                //
                                //Marshal.FreeHGlobal(outit);
                                break;
                            case "getdata_dapai":
                                ido.i_setData(ref t);
                                break;

                        }

                        break;

                }
            }
            return 1;
        }//返回string/*^分割*/
         /// <summary>
         /// 吴龙剑麻将函数（delphi dll调用）
         /// </summary>
         /// <param name="dowhat"></param>
         /// <param name="datas"></param>
         /// <param name="t"></param>
         /// <returns></returns>
        unsafe public int doMsg_fun(string dowhat/*^分割*/, string datas/*结构体地址 ^分割*/, ref tr_rltpchar t)//返回string/*^分割*/
        {
            switch (dowhat) {
                case "Log":
                    MrWu.Debug.Debug.Log("exe log: " + datas, false);
                    break;
                case "errorLog":

                    MrWu.Debug.Debug.Error("exe wrong: " + datas, true);
                    break;
                case "zyxfun":
                    string[] ss = datas.Split('~');
                    t.str = "slkfj234";
                    t.str = doMsg(ss[0], ss[1]);
                    if (t.str == null || t.str == "" || t.str == " ")
                        log("wrong sdf", true);
                    break;
                default:
                    log("wrong ...sdfsd", true);
                    break;

            }
            return 0;
        }

        unsafe public string doMsg_other(string dowhat/*^分割*/, string datas/*结构体地址 ^分割*/)//返回string/*^分割*/
        {



            string result = "";
            string[] dts = null;
            if (datas != null) dts = datas.Split('^');
            string[] dostr = dowhat.Split('^');

            string[] cmds = dostr[0].Split('*');
            //			if (cls_nativeCom.showlog && cmds[0]!="dotimer")
            //				log("domsg = "+dowhat+"  "+datas);
            switch (cmds[0]) {
                case "changeText":
                    MrWu.Debug.Debug.Log("changeText:" + cmds[1]);
                    break;
                case "terminate":
                    MrWu.Debug.Debug.Error("切换游戏状态");
                    // baseinfo.instance.exe.gamestate = 4;
                    //log("termainte self...");
                    //	this.timer1.Enabled=true;
                    break;
                case "serstate":
                    MrWu.Debug.Debug.Error("切换游戏状态：");
                    //baseinfo.instance.exe.gamestate = int.Parse(cmds[1]);
                    break;
                case "showcsharpdllform":
                    MrWu.Debug.Debug.Log("显示dll窗口!");
                    /*
                    Form fmdll = baseinfo.instance.dll.fm;
                    if (fmdll == null) break;
                    if (cmds[1] == "1") {

                        fmdll.Show();
                    } else {
                        if (baseinfo.instance.exe.gamestate == 3) {

                            fmdll.Close();
                        } else {
                            if (baseinfo.instance.dll.fmsub != null)
                                baseinfo.instance.dll.fmsub.Hide();
                            fmdll.Hide();

                        }
                    }
                    */

                    break;

                case "yasuo":
                    ABlock ablockTmp = new ABlock();
                    cls_nativeCom.Instance.getDataOfStruct<ABlock>(int.Parse(dts[0]), ref ablockTmp, (int*)&ablockTmp);
                    ABlock ablockTmp1 = new ABlock();
                    cls_nativeCom.Instance.getDataOfStruct<ABlock>(int.Parse(dts[1]), ref ablockTmp1, (int*)&ablockTmp1);
                    //	log("xxxx    "+ablockTmp.len+"    "+ablockTmp1.len);
                    if (cmds[1][0] == '1') result = cls_nativeCom.Instance.Compress(ref ablockTmp, ref ablockTmp1).ToString();
                    else
                        result = cls_nativeCom.Instance.deCompress(ref ablockTmp, ref ablockTmp1).ToString();
                    //log("xxxx xxx   "+ablockTmp.len+"    "+ablockTmp1.len);
                    break;
                case "gameClose":
                    return "closeOk";//必须
                case "dopack_ser"://收包

                    int regid_ = int.Parse(dostr[1]);
                    int blx_ = int.Parse(dostr[2]);//包类型--编号
                    int addr_mainpack = int.Parse(dts[0]);//包内容
                    break;
                    //				case "dopack"://收包
                    //
                    //					int blx=int.Parse(dostr[2]);//包类型--编号
                    //					int dataAddr=int.Parse(dostr[3]);//数据地址
                    //					int dataSize=int.Parse(dostr[4]);//数据大小
                    //					int regid=int.Parse(dostr[5]);//谁发的包
                    //					int addr_player=int.Parse(dostr[6]);//谁的地址
                    //
                    //
                    //
                    //					switch (blx){//40000~99999给c#新启用
                    //						case 50000://test
                    //							string str1=myCom.Instance. getUnmanagedString(dataAddr,dataSize);
                    //							log(blx+"  "+str1);
                    //							break;
                    //						case 50001:
                    //							stu_test st = new stu_test();
                    //							myCom.Instance.getDataOfStruct(dataAddr, ref st,(int*)&st);
                    //							log(blx+"  "+st.b+"");
                    //							break;
                    ////						case 50002:
                    ////							mytestCls1 cls=(mytestCls1)myCom.Instance.getDataOfClass(dataAddr,dataSize);
                    ////							log(blx+"  "+cls.b+"");
                    ////							break;
                    //
                    //					}
                    //					break;
            }
            return result;

        }

        static int errorIdx;
        unsafe public virtual string doMsg(string dowhat/*^分割*/, string datas/*结构体地址 ^分割*/)//返回string/*^分割*/
        { return "defautSTR"; }
    }
}