Beispiel #1
0
    public void Handle_MCU_Connect()
    {
        //从FRAM获得guid
        byte[] guidFRAMByte = null;
        if (mMCU.Read_Block(0, 16, out guidFRAMByte))//从FRAM读取guid成功
        {
            Debug.Log("open guid:" + NemoSerial.ByteArrayToString(StaticValueContainerDBFRAM.GetDBID().ToByteArray()));
            System.Guid FRAMEGUID = new System.Guid(guidFRAMByte);
            if (StaticValueContainerDBFRAM.GetDBID() != System.Guid.Empty &&
                FRAMEGUID != StaticValueContainerDBFRAM.GetDBID())   //FRAM和db的guid不一致
            {
                Debug.Log("open guid不一致:" + NemoSerial.ByteArrayToString(StaticValueContainerDBFRAM.GetDBID().ToByteArray()));
                mMCU.RequestReadWrite(true, 0, 16, StaticValueContainerDBFRAM.GetDBID().ToByteArray());//将dbid写入FRAM
                //更新所有IUpdatable
                foreach (IUpdatable i in Updatables)
                {
                    i.UpdateToFRAM();
                }
            }
        }

        //if(StaticValueContainerDBFRAM.GetDBID()
    }
Beispiel #2
0
    public HotFileDbFRAMIO(string key, string filePath)
    {
        if (GameMain.Singleton != null)
        {
            mMCU = GameMain.Singleton.ArcIO;
        }
        if (mMCU == null)
        {
            mMCU = INemoControlIOSinglton.Get();
        }
        //?????????????
        if (mConn == null)
        {
            if (DataBaseName == "")
            {
                DataBaseName = "GameDB.db";
            }

            string dataPath = System.Environment.CurrentDirectory + "/DataFiles";
            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }


            mConn = new SqliteConnection("Data Source = DataFiles/" + DataBaseName + ";");
            mConn.Open();

            if (mCMD == null)
            {
                mCMD = mConn.CreateCommand();
                mCMD.Parameters.Add(new SqliteParameter());
            }

            mCMD.CommandText = "PRAGMA journal_mode =wal;";//wal??
            mCMD.ExecuteNonQuery();
        }



        mKey    = key;
        mDBType = GetDBType();
        //bool isInitFRAMByDB = false;
        //?????DB_FARM_ID
        if (mMCU.IsOpen() && DB_ID == System.Guid.Empty)
        {
            using (SqliteCommand cmd = mConn.CreateCommand())
            {
                cmd.CommandText = "CREATE TABLE IF NOT EXISTS _DB_GUID(Val BLOB);";
                //cmd.Parameters.Add("@data", System.Data.DbType.Binary).Value = System.Guid.Empty.ToByteArray();
                cmd.ExecuteNonQuery();
                cmd.CommandText = string.Format(" SELECT * FROM _DB_GUID;");
                SqliteDataReader r = cmd.ExecuteReader();
                r.Read();

                if (r.HasRows)//????????????????
                {
                    //???????????guid?
                    DB_ID = new System.Guid(r.GetValue(0) as byte[]);
                    //???FRAM??guid?
                    byte[] guidFRAMByte = null;
                    if (mMCU.Read_Block(0, 16, out guidFRAMByte))//??FRAM???guid???
                    {
                        System.Guid FRAMEGUID = new System.Guid(guidFRAMByte);
                        if (FRAMEGUID != DB_ID)                                      //FRAM??db??guid?????
                        {
                            mMCU.RequestReadWrite(true, 0, 16, DB_ID.ToByteArray()); //??dbid§Õ??FRAM
                            IsInitFRAMByDB = true;
                        }
                    }
                    r.Close();
                }
                else//?????????guid
                {
                    r.Close();
                    //??????dbGUID
                    DB_ID = System.Guid.NewGuid();
                    byte[] guidByteNew = DB_ID.ToByteArray();
                    //????db
                    cmd.CommandText = "INSERT INTO _DB_GUID VALUES (@data)";
                    cmd.Parameters.Add("@data", System.Data.DbType.Binary).Value = guidByteNew;
                    cmd.ExecuteNonQuery();

                    //?????????guid ????FRAM
                    mMCU.RequestReadWrite(true, 0, 16, guidByteNew);
                    IsInitFRAMByDB = true;
                }
            }
        }


        //??????(4294967295 == 0xffffffff)
        mCMD.CommandText = string.Format("CREATE TABLE IF NOT EXISTS {0:s}(Val {1:s} KEY DEFAULT {2:s},Address INTEGER DEFAULT 4294967295,Length INTEGER DEFAULT {3:d});", mKey, GetDBTypeString(), GetDefaultDBValString(), GetValLength());
        mCMD.ExecuteNonQuery();


        //???????????????
        mCMD.CommandText = string.Format(" SELECT * FROM {0:s};", mKey);
        SqliteDataReader reader = mCMD.ExecuteReader();

        reader.Read();

        //
        //System.Guid.NewGuid();

        if (reader.HasRows)//????????????????
        {
            //????????????????????????
            mAddress    = (uint)reader.GetInt64(1);
            mDataLength = (byte)reader.GetInt32(2);
            if (IsInitFRAMByDB)
            {
                WriteToFRAM(ObjectToVal(reader.GetValue(0)));
            }
            reader.Close();
        }
        else//????????????????,????????
        {
            mAddress    = CurrentUseAddress;
            mDataLength = (byte)GetValLength();
            reader.Close();

            //???????????
            mCMD.CommandText   = string.Format("INSERT INTO {0:s} VALUES({1:s},{2:d},{3:d})", mKey, GetDefaultDBValString(), mAddress, mDataLength);
            CurrentUseAddress += GetValLength();//???????
            mCMD.ExecuteNonQuery();

            //?????farm
            WriteToFRAM(GetDefaultVal());
        }

        mUpdateSQLText = "UPDATE " + mKey + " SET Val = ?;";

        ++SqliteConnRefCount;
        HotFileDBIOReleaser.HotFileDBReg(this);
        HotFileDBFRAM_Updater.Reg(this);
    }