예제 #1
0
        /// <summary>
        /// Registers a PAction with the action manager. There is no corresponding Unregister
        /// call, so avoid spamming PActions.
        ///
        /// This call should occur after PUtil.InitLibrary() during the mod OnLoad(). If called
        /// earlier, it may fail with InvalidOperationException, and if called later, the
        /// user's custom key bind (if applicable) will be discarded.
        /// </summary>
        /// <param name="identifier">The identifier for this action.</param>
        /// <param name="title">The action's title.</param>
        /// <param name="binding">The default key binding for this action.</param>
        /// <returns>The action thus registered.</returns>
        /// <exception cref="InvalidOperationException">If PLib is not yet initialized.</exception>
        public static PAction Register(string identifier, LocString title,
                                       PKeyBinding binding = null)
        {
            // In case this call is used before the library was initialized
            if (!PUtil.PLibInit)
            {
                PUtil.InitLibrary(false);
                PUtil.LogWarning("PUtil.InitLibrary was not called before using " +
                                 "PAction.Register!");
            }
            int     actionID;
            PAction action;

            lock (PSharedData.GetLock(PRegistry.KEY_ACTION_LOCK)) {
                actionID = PSharedData.GetData <int>(PRegistry.KEY_ACTION_ID);
                if (actionID <= 0)
                {
                    throw new InvalidOperationException("PAction action ID is not set!");
                }
                PSharedData.PutData(PRegistry.KEY_ACTION_ID, actionID + 1);
            }
            action = new PAction(actionID, identifier, title);
            PActionManager.ConfigureTitle(action);
            action.AddKeyBinding(binding ?? new PKeyBinding());
            return(action);
        }
예제 #2
0
        /// <summary>
        /// Registers a PAction with the action manager. There is no corresponding Unregister
        /// call, so avoid spamming PActions.
        ///
        /// This call should occur after PUtil.LogModInit() during the mod OnLoad(). If called
        /// earlier, it may fail with InvalidOperationException, and if called later, the
        /// user's custom key bind (if applicable) will be discarded.
        /// </summary>
        /// <param name="identifier">The identifier for this action.</param>
        /// <param name="title">The action's title.</param>
        /// <param name="binding">The default key binding for this action.</param>
        /// <returns>The action thus registered.</returns>
        /// <exception cref="InvalidOperationException">If PLib is not yet initialized.</exception>
        public static PAction Register(string identifier, LocString title,
                                       PKeyBinding binding = null)
        {
            object locker = PSharedData.GetData <object>(PRegistry.KEY_ACTION_LOCK);
            int    actionID;

            if (locker == null)
            {
                throw new InvalidOperationException("PAction.Register called before PLib loaded!");
            }
            PAction action;

            lock (locker) {
                actionID = PSharedData.GetData <int>(PRegistry.KEY_ACTION_ID);
                if (actionID <= 0)
                {
                    throw new InvalidOperationException("PAction action ID is not set!");
                }
                PSharedData.PutData(PRegistry.KEY_ACTION_ID, actionID + 1);
            }
            action = new PAction(actionID, identifier, title);
            PActionManager.ConfigureTitle(action);
            action.AddKeyBinding(binding ?? new PKeyBinding());
            return(action);
        }