ScriptName aaaKimachuuDisableHelper Extends ActiveMagicEffect

Import ConsoleUtil
Import PapyrusIniManipulator
Import PO3_SKSEFunctions
Import SkyMessage
Import StringUtil

String strPath = "Data/Kimachuu Disable Helper_SWAP.ini"

Event OnEffectStart(Actor akTarget, Actor akCaster)
	ObjectReference TargetRef = GetSelectedReference()

	If !TargetRef
		Debug.Notification("Nothing is selected in the console.")
	Else
		Int iFID = TargetRef.GetFormID() ;10進数で取得
		String strModName = GetFormModName(TargetRef, False) ;esp名を取得する
		String strResult = Show(strModName + ":" + TargetRef, "Cancel", "Delete")
		
		If strResult == "Delete"
			String strHex = DecimalToHex(iFID) ;RefIDを16進数に変換
			String strRID
			Int iCheck = LoadOrderCheck(iFID)
			If iCheck == 1
				Debug.MessageBox("This object is temporary.")
				Return
			ElseIf iCheck == 2
				strRID = Substring(strHex, 5)
			ElseIf iCheck == 3
				strRID = Substring(strHex, 2)
			EndIf
			strHex = "0x" + strRID
			PushStringToIni(strPath, "References", strHex + "~" + strModName + "|0x58E92~Skyrim.esm|posA(1.0,1.0,-30000.0),flags(0x00000800)", "DELETEME", True)
			TargetRef.Disable()
			TargetRef.Delete()
		EndIf
	EndIf
EndEvent

String Function DecimalToHex(Int aiDecimal)
	String strHex

	Int i = 0
	While i < 8
		strHex = StringUtil.GetNthChar("0123456789ABCDEF", Math.LogicalAnd(0xF, aiDecimal)) + strHex
		aiDecimal = Math.RightShift(aiDecimal, 4)
		i += 1
	EndWhile

	Return strHex
EndFunction

Int Function LoadOrderCheck(Int aiFormID)
    Int intOrder = Math.RightShift(aiFormID, 24)
    If intOrder == 0xFF
        Return 1
    ElseIf intOrder == 0xFE
        Return 2
	Else
		Return 3
    EndIf
EndFunction