This class encapsulates a script-managed database connection.
        /// <summary>
        /// Create a new database connection object and return a handle to it.
        /// </summary>
        /// <param name="ConnectionString">Supplies the connection string.
        /// </param>
        /// <param name="Flags">Supplies control flags.</param>
        /// <returns>The connection handle.</returns>
        public static int CreateScriptDatabaseConnection(string ConnectionString, ScriptDatabaseConnectionFlags Flags)
        {
            int Handle = ++NextConnectionHandle;
            ScriptDatabaseConnection Connection = new ScriptDatabaseConnection(ConnectionString, Flags);

            if (Handle == 0)
                Handle = ++NextConnectionHandle;

            ConnectionTable.Add(Handle, Connection);

            return Handle;
        }
Пример #2
0
        /// <summary>
        /// Create a new database connection object and return a handle to it.
        /// </summary>
        /// <param name="ConnectionString">Supplies the connection string.
        /// </param>
        /// <param name="Flags">Supplies control flags.</param>
        /// <returns>The connection handle.</returns>
        public static int CreateScriptDatabaseConnection(string ConnectionString, ScriptDatabaseConnectionFlags Flags)
        {
            int Handle;
            ScriptDatabaseConnection Connection;

            if (Flags.HasFlag(ScriptDatabaseConnectionFlags.StandardConnection))
            {
                Handle = 0;

                if (StandardConnection == null)
                {
                    ConnectionString              = GetStandardDatabaseConnectionString();
                    Connection                    = new ScriptDatabaseConnection(ConnectionString, Flags);
                    StandardConnection            = Connection;
                    ModuleLinkage.DefaultDatabase = StandardConnection.Database;
                    ConnectionTable.Add(Handle, Connection);
                }
            }
            else
            {
                Connection = new ScriptDatabaseConnection(ConnectionString, Flags);
                Handle     = ++NextConnectionHandle;

                if (Handle == 0)
                {
                    Handle = ++NextConnectionHandle;
                }

                ConnectionTable.Add(Handle, Connection);
            }

            if (Flags.HasFlag(ScriptDatabaseConnectionFlags.StandardQueryLog) && StandardQueryLogger == null)
            {
                try
                {
                    StandardQueryLogger = new StreamWriter(ALFA.SystemInfo.GetNWNX4InstallationDirectory() + "StandardQueryLog.txt");
                }
                catch (Exception e)
                {
                    Logger.Log("ScriptDatabaseConnection.CreateScriptDatabaseConnection: Couldn't open standard query log (StandardQueryLog.txt in NWNX4 directory): Exception: {0}", e);
                }
            }

            return(Handle);
        }
        /// <summary>
        /// Create a new database connection object and return a handle to it.
        /// </summary>
        /// <param name="ConnectionString">Supplies the connection string.
        /// </param>
        /// <param name="Flags">Supplies control flags.</param>
        /// <returns>The connection handle.</returns>
        public static int CreateScriptDatabaseConnection(string ConnectionString, ScriptDatabaseConnectionFlags Flags)
        {
            int Handle;
            ScriptDatabaseConnection Connection;

            if (Flags.HasFlag(ScriptDatabaseConnectionFlags.StandardConnection))
            {
                Handle = 0;

                if (StandardConnection == null)
                {
                    ConnectionString = GetStandardDatabaseConnectionString();
                    Connection = new ScriptDatabaseConnection(ConnectionString, Flags);
                    StandardConnection = Connection;
                    ModuleLinkage.DefaultDatabase = StandardConnection.Database;
                    ConnectionTable.Add(Handle, Connection);
                }
            }
            else
            {
                Connection = new ScriptDatabaseConnection(ConnectionString, Flags);
                Handle = ++NextConnectionHandle;

                if (Handle == 0)
                    Handle = ++NextConnectionHandle;

                ConnectionTable.Add(Handle, Connection);
            }

            if (Flags.HasFlag(ScriptDatabaseConnectionFlags.StandardQueryLog) && StandardQueryLogger == null)
            {
                try
                {
                    StandardQueryLogger = new StreamWriter(ALFA.SystemInfo.GetNWNX4InstallationDirectory() + "StandardQueryLog.txt");
                }
                catch (Exception e)
                {
                    Logger.Log("ScriptDatabaseConnection.CreateScriptDatabaseConnection: Couldn't open standard query log (StandardQueryLog.txt in NWNX4 directory): Exception: {0}", e);
                }
            }

            return Handle;
        }
        public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode)
        {
            Int32  ReturnCode;
            int    RequestType = (int)ScriptParameters[0];
            int    P0          = (int)ScriptParameters[1];
            int    P2          = (int)ScriptParameters[2];
            string P3          = (string)ScriptParameters[3];
            string P4          = (string)ScriptParameters[4];
            string P5          = (string)ScriptParameters[5];
            uint   P6          = (uint)ScriptParameters[6];

            switch ((REQUEST_TYPE)RequestType)
            {
            case REQUEST_TYPE.EXECUTE_UPDATER_SCRIPT:
            {
                ReturnCode = ExecuteUpdaterScript() ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.CREATE_AREA_INSTANCE:
            {
                uint ReturnArea = CreateAreaInstance(P6);

                if (ReturnArea == OBJECT_INVALID)
                {
                    ReturnCode = FALSE;
                }
                else
                {
                    ReturnCode = TRUE;
                    SetLocalObject(GetModule(), "ACR_SERVER_MISC_RETURN_OBJECT", ReturnArea);
                }
            }
            break;

            case REQUEST_TYPE.RELEASE_AREA_INSTANCE:
            {
                ReleaseInstancedArea(P6);
                ReturnCode = TRUE;
            }
            break;

            case REQUEST_TYPE.SET_DICTIONARY_VALUE:
            {
                DictionarySetString(P3, P4, P5);
                ReturnCode = TRUE;
            }
            break;

            case REQUEST_TYPE.GET_DICTIONARY_VALUE:
            {
                string ReturnValue;
                bool   CompletedOk = DictionaryGetString(P3, P4, out ReturnValue);

                if (!CompletedOk)
                {
                    ReturnCode = FALSE;
                }
                else
                {
                    SetLocalString(GetModule(), "ACR_SERVER_MISC_RETURN_STRING", ReturnValue);
                    ReturnCode = TRUE;
                }
            }
            break;

            case REQUEST_TYPE.FIRST_ITERATE_DICTIONARY:
            {
                string ReturnKey;
                bool   CompletedOk = DictionaryIterateFirst(P3, out ReturnKey);

                if (!CompletedOk)
                {
                    ReturnCode = FALSE;
                }
                else
                {
                    SetLocalString(GetModule(), "ACR_SERVER_MISC_RETURN_STRING", ReturnKey);
                    ReturnCode = TRUE;
                }
            }
            break;

            case REQUEST_TYPE.NEXT_ITERATE_DICTIONARY:
            {
                string ReturnKey;
                bool   CompletedOk = DictionaryIterateNext(P3, out ReturnKey);

                if (!CompletedOk)
                {
                    ReturnCode = FALSE;
                }
                else
                {
                    SetLocalString(GetModule(), "ACR_SERVER_MISC_RETURN_STRING", ReturnKey);
                    ReturnCode = TRUE;
                }
            }
            break;

            case REQUEST_TYPE.DELETE_DICTIONARY_KEY:
            {
                DictionaryDeleteKey(P3, P4);
                ReturnCode = TRUE;
            }
            break;

            case REQUEST_TYPE.CLEAR_DICTIONARY:
            {
                DictionaryClear(P3);
                ReturnCode = TRUE;
            }
            break;

            case REQUEST_TYPE.RUN_POWERSHELL_SCRIPTLET:
            {
                ReturnCode = RunPowerShellScriptlet(P3, P6) ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.CREATE_DATABASE_CONNECTION:
            {
                ReturnCode = ScriptDatabaseConnection.CreateScriptDatabaseConnection(P3, (ScriptDatabaseConnectionFlags)P0);
            }
            break;

            case REQUEST_TYPE.DESTROY_DATABASE_CONNECTION:
            {
                ReturnCode = ScriptDatabaseConnection.DestroyDatabaseConnection(P0) ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.QUERY_DATABASE_CONNECTION:
            {
                ReturnCode = ScriptDatabaseConnection.QueryDatabaseConnection(P0, P3, this) ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.FETCH_DATABASE_CONNECTION:
            {
                ReturnCode = ScriptDatabaseConnection.FetchDatabaseConnection(P0, this) ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.GET_COLUMN_DATABASE_CONNECTION:
            {
                string Data = ScriptDatabaseConnection.GetColumnDatabaseConnection(P0, P2, this);

                if (Data == null)
                {
                    ReturnCode = FALSE;
                }
                else
                {
                    SetLocalString(GetModule(), "ACR_SERVER_MISC_RETURN_STRING", Data);
                    ReturnCode = TRUE;
                }
            }
            break;

            case REQUEST_TYPE.GET_AFFECTED_ROW_COUNT_DATABASE_CONNECTION:
            {
                ReturnCode = ScriptDatabaseConnection.GetAffectedRowCountDatabaseConnection(P0, this);
            }
            break;

            case REQUEST_TYPE.ESCAPE_STRING_DATABASE_CONNECTION:
            {
                string Data = ScriptDatabaseConnection.EscapeStringDatabaseConnection(P0, P3, this);

                if (Data == null)
                {
                    ReturnCode = FALSE;
                }
                else
                {
                    SetLocalString(GetModule(), "ACR_SERVER_MISC_RETURN_STRING", Data);
                    ReturnCode = TRUE;
                }
            }
            break;

            case REQUEST_TYPE.GET_STACK_TRACE:
            {
                StackTrace Trace = new StackTrace(true);

                SetLocalString(GetModule(), "ACR_SERVER_MISC_RETURN_STRING", Trace.ToString());
                ReturnCode = TRUE;
            }
            break;

            case REQUEST_TYPE.GET_SALTED_MD5:
            {
                string SaltedMD5 = GetSaltedMD5(P3);

                SetLocalString(GetModule(), "ACR_SERVER_MISC_RETURN_STRING", SaltedMD5);
                ReturnCode = TRUE;
            }
            break;

            case REQUEST_TYPE.GET_HAS_DATABASE_STORE:
            {
                string Campaign = P3;

                ReturnCode = CampaignObjectFileStore.GetHasDatabaseStore(Campaign) ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.DELETE_DATABASE_STORE:
            {
                string Campaign = P3;

                ReturnCode = CampaignObjectFileStore.DeleteDatabaseStore(Campaign) ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.DELETE_DATABASE_STORE_AT_INDEX:
            {
                string Campaign = P3;
                int    Index    = P0;

                ReturnCode = CampaignObjectFileStore.DeleteDatabaseStoreAtIndex(Campaign, Index) ? TRUE : FALSE;
            }
            break;

            case REQUEST_TYPE.SHOW_COMPILER_LOG:
            {
                string FileName = String.Format("{0}{1}ALFAModuleRecompile.log", Path.GetTempPath(), Path.DirectorySeparatorChar);

                SendMessageToPC(OBJECT_SELF, "Last module recompilation log:");

                try
                {
                    SendMessageToPC(OBJECT_SELF, File.ReadAllText(FileName));
                }
                catch (Exception)
                {
                    SendMessageToPC(OBJECT_SELF, "<no log file exists>");
                }

                ReturnCode = TRUE;
            }
            break;

            case REQUEST_TYPE.RESTART_SERVER:
            {
                ALFA.SystemInfo.ShutdownGameServer(this);
                ReturnCode = TRUE;
            }
            break;

            default:
                throw new ApplicationException("Invalid server misc command " + RequestType.ToString());
            }

            return(ReturnCode);
        }