├── .gitattributes ├── .gitignore ├── README.md ├── kikokugai ├── function - 副本.nss ├── function.nss ├── npapk.py ├── patch │ ├── .vscode │ │ └── settings.json │ ├── CMakeLists.txt │ ├── loader.cpp │ ├── patch.cpp │ └── patch.rc └── solve.py └── muramasa ├── patch ├── .vscode │ └── settings.json ├── CMakeLists.txt ├── Detours-4.0.1 │ ├── CREDITS.TXT │ ├── LICENSE.md │ ├── Makefile │ ├── README.md │ ├── include │ │ ├── detours.h │ │ ├── detver.h │ │ └── syelog.h │ ├── lib.X86 │ │ ├── detours.lib │ │ ├── detours.pdb │ │ └── syelog.lib │ └── system.mak ├── loader.cpp ├── patch.cpp └── patch.rc └── solve.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | nss 2 | muramasa/patch/build 3 | nss.npa 4 | nss-1.npa 5 | kikokugai/patch/build 6 | kikokugai/nss-1-h 7 | kikokugai/nss-1 8 | *.npa 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### [装甲恶鬼村正](https://github.com/HIllya51/MuramasaHorizontal/releases/download/release/zjegcz_h.zip)和[鬼哭街](https://github.com/HIllya51/MuramasaHorizontal/releases/download/release/kikokugai_h.zip)的横向文字补丁 -------------------------------------------------------------------------------- /kikokugai/function - 副本.nss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HIllya51/MuramasaHorizontal/f4adbb1ec7ea5bdc0796ace514174d0f0a297207/kikokugai/function - 副本.nss -------------------------------------------------------------------------------- /kikokugai/function.nss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HIllya51/MuramasaHorizontal/f4adbb1ec7ea5bdc0796ace514174d0f0a297207/kikokugai/function.nss -------------------------------------------------------------------------------- /kikokugai/npapk.py: -------------------------------------------------------------------------------- 1 | #encoding=utf-8 2 | #py2.7 3 | import os 4 | from struct import pack 5 | from pdb import set_trace as int3 6 | import zlib 7 | 8 | #鬼哭街 9 | DecTable=[ 10 | 0x21, 0xE1, 0xA0, 0x8D, 0x86, 0xF1, 0xE3, 0xE9, 0x00, 0xB6, 0x2D, 0xD7, 0x6A, 0x43, 0x93, 0xB0, 0xEC, 0x0A, 0xCF, 0x37, 0xB8, 0x3E, 0x9E, 0xF9, 0x9A, 0x4D, 0xC8, 0x7D, 0x5A, 0x77, 0x3A, 0x3D, 11 | 0x12, 0x27, 0x17, 0xCA, 0x89, 0x8F, 0x6C, 0x9C, 0x5F, 0x0F, 0x40, 0x64, 0xCD, 0x20, 0x3F, 0xFD, 0xB9, 0x73, 0xBE, 0x30, 0xEB, 0xAD, 0xB3, 0x10, 0xEE, 0x38, 0x7A, 0xFA, 0xD5, 0x4B, 0x5D, 0xB5, 12 | 0xE6, 0x91, 0xA5, 0xE7, 0xDA, 0xAA, 0x75, 0x0D, 0x3C, 0x9F, 0xF8, 0x7E, 0xB2, 0xA4, 0x62, 0xAB, 0xCB, 0x03, 0x6E, 0x18, 0xE5, 0x46, 0xC1, 0x4E, 0xFF, 0x63, 0x01, 0x97, 0xE2, 0x1A, 0x76, 0x65, 13 | 0x49, 0xCC, 0x1C, 0xBB, 0x09, 0x54, 0x58, 0xC9, 0x87, 0x05, 0xF2, 0x14, 0x52, 0xDC, 0x23, 0xF0, 0xDF, 0x7F, 0xED, 0x04, 0xAC, 0x16, 0xA8, 0x0E, 0x1F, 0xAE, 0xBA, 0x66, 0x6F, 0x5E, 0xD2, 0x96, 14 | 0xFE, 0x61, 0x69, 0xB7, 0x55, 0xA7, 0x45, 0x1D, 0xB1, 0x80, 0x2A, 0xC4, 0x70, 0x6D, 0xF5, 0x9D, 0xD4, 0xDE, 0x79, 0x74, 0x07, 0xC2, 0x2B, 0xA9, 0xBC, 0x25, 0x13, 0x5B, 0x15, 0x22, 0x06, 0x8C, 15 | 0x82, 0xF3, 0x29, 0x94, 0xA1, 0xA6, 0x7B, 0x9B, 0xD8, 0xA3, 0x98, 0x1E, 0x11, 0x1B, 0x35, 0x59, 0x02, 0xFB, 0x99, 0xD6, 0xB4, 0x72, 0xD3, 0x4C, 0x90, 0xC0, 0xBF, 0x2C, 0xEF, 0x8B, 0x88, 0xFC, 16 | 0x53, 0x51, 0x85, 0xD1, 0xDB, 0x56, 0x4A, 0xF4, 0x19, 0x92, 0x33, 0x42, 0xC3, 0x36, 0x26, 0x60, 0x2E, 0xD0, 0xAF, 0x83, 0xF7, 0x48, 0x78, 0x4F, 0x95, 0x08, 0xC6, 0x32, 0xCE, 0x3B, 0x34, 0xD9, 17 | 0x57, 0x5C, 0xDD, 0xE8, 0x31, 0xC7, 0x7C, 0xE4, 0x67, 0x71, 0x50, 0x0B, 0x81, 0x44, 0x0C, 0xC5, 0x24, 0x8A, 0x41, 0x8E, 0xE0, 0x39, 0x2F, 0xA2, 0x68, 0x6B, 0xEA, 0xF6, 0x84, 0xBD, 0x28, 0x47, 18 | ] 19 | 20 | '''#Axanael 21 | DecTable=[ 22 | 0x21, 0x71, 0xF0, 0xD8, 0xD6, 0x81, 0x73, 0x7C, 0x00, 0x46, 0x28, 0x67, 0xBD, 0x93, 0xE3, 0x40, 0x7F, 0x0D, 0x5A, 0x37, 0x4B, 0x39, 0xE9, 0x8C, 0xED, 0x98, 0x5B, 0xC8, 0xAD, 0xC7, 0x3D, 0x38, 23 | 0x12, 0x27, 0x17, 0x5D, 0xDC, 0xDA, 0xBF, 0xEF, 0xAA, 0x0A, 0x90, 0xB4, 0x58, 0x20, 0x3A, 0x88, 0x4C, 0xC3, 0x49, 0x30, 0x7E, 0xF8, 0x43, 0x10, 0x79, 0x3B, 0xCD, 0x8D, 0x65, 0x9E, 0xA8, 0x45, 24 | 0x76, 0xE1, 0xF5, 0x77, 0x6D, 0xFD, 0xC5, 0x08, 0x3F, 0xEA, 0x8B, 0xC9, 0x42, 0xF4, 0xB2, 0xFE, 0x5E, 0x03, 0xB9, 0x1B, 0x75, 0x96, 0x51, 0x99, 0x8A, 0xB3, 0x01, 0xE7, 0x72, 0x1D, 0xC6, 0xB5, 25 | 0x9C, 0x5F, 0x1F, 0x4E, 0x0C, 0xA4, 0xAB, 0x5C, 0xD7, 0x05, 0x82, 0x14, 0xA2, 0x6F, 0x23, 0x80, 0x6A, 0xCA, 0x78, 0x04, 0xFF, 0x16, 0xFB, 0x09, 0x1A, 0xF9, 0x4D, 0xB6, 0xBA, 0xA9, 0x62, 0xE6, 26 | 0x89, 0xB1, 0xBC, 0x47, 0xA5, 0xF7, 0x95, 0x18, 0x41, 0xD0, 0x2D, 0x54, 0xC0, 0xB8, 0x85, 0xE8, 0x64, 0x69, 0xCC, 0xC4, 0x07, 0x52, 0x2E, 0xFC, 0x4F, 0x25, 0x13, 0xAE, 0x15, 0x22, 0x06, 0xDF, 27 | 0xD2, 0x83, 0x2C, 0xE4, 0xF1, 0xF6, 0xCE, 0xEE, 0x6B, 0xF3, 0xEB, 0x19, 0x11, 0x1E, 0x35, 0xAC, 0x02, 0x8E, 0xEC, 0x66, 0x44, 0xC2, 0x63, 0x9F, 0xE0, 0x50, 0x4A, 0x2F, 0x7A, 0xDE, 0xDB, 0x8F, 28 | 0xA3, 0xA1, 0xD5, 0x61, 0x6E, 0xA6, 0x9D, 0x84, 0x1C, 0xE2, 0x33, 0x92, 0x53, 0x36, 0x26, 0xB0, 0x29, 0x60, 0xFA, 0xD3, 0x87, 0x9B, 0xCB, 0x9A, 0xE5, 0x0B, 0x56, 0x32, 0x59, 0x3E, 0x34, 0x6C, 29 | 0xA7, 0xAF, 0x68, 0x7B, 0x31, 0x57, 0xCF, 0x74, 0xB7, 0xC1, 0xA0, 0x0E, 0xD1, 0x94, 0x0F, 0x55, 0x24, 0xDD, 0x91, 0xD9, 0x70, 0x3C, 0x2A, 0xF2, 0xBB, 0xBE, 0x7D, 0x86, 0xD4, 0x48, 0x2B, 0x97, 30 | ]''' 31 | 32 | # #君と彼女と彼女の恋 33 | # DecTable=[ 34 | # 0x57, 0xD7, 0x66, 0x10, 0x14, 0xE7, 0xDF, 0xD8, 0x36, 0x74, 0x50, 0x95, 0xC9, 0xAF, 0x2F, 0x76, 0xDD, 0x39, 0x82, 0xF5, 0x7A, 0xF1, 0x21, 0xE8, 0x29, 0xA0, 0x8A, 0x00, 0xB9, 0x05, 0xF9, 0xF0, 35 | # 0x4B, 0x55, 0x45, 0x89, 0x18, 0x12, 0xCD, 0x2D, 0xB2, 0x32, 0xA6, 0xC3, 0x80, 0x56, 0xF2, 0xE0, 0x78, 0x0F, 0x71, 0xF6, 0xDC, 0x60, 0x7F, 0x46, 0xD1, 0xFA, 0x09, 0xE9, 0x9E, 0xAC, 0xB0, 0x7E, 36 | # 0xD4, 0x27, 0x6E, 0xD5, 0x99, 0x69, 0x0E, 0x30, 0xFD, 0x22, 0xEA, 0x01, 0x7B, 0x63, 0xCB, 0x6C, 0x8C, 0x3F, 0xC1, 0x4A, 0xDE, 0xA4, 0x87, 0xA1, 0xE2, 0xCF, 0x37, 0x25, 0xDB, 0x49, 0x04, 0xCE, 37 | # 0xA8, 0x8D, 0x4D, 0x7C, 0x38, 0xB3, 0xBA, 0x88, 0x15, 0x3E, 0xEB, 0x43, 0xBB, 0x9D, 0x5F, 0xE6, 0x92, 0x02, 0xD0, 0x33, 0x6D, 0x44, 0x6A, 0x31, 0x42, 0x61, 0x79, 0xC4, 0xC2, 0xB1, 0x9B, 0x24, 38 | # 0xE1, 0xC7, 0xC8, 0x75, 0xBE, 0x65, 0xAE, 0x40, 0x77, 0x16, 0x59, 0x83, 0x06, 0xC0, 0xEE, 0x20, 0x93, 0x91, 0x08, 0x03, 0x35, 0x8B, 0x5C, 0x68, 0x7D, 0x5E, 0x4F, 0xBC, 0x4E, 0x5B, 0x34, 0x1D, 39 | # 0x1B, 0xEF, 0x58, 0x23, 0x67, 0x64, 0x0C, 0x2C, 0x9A, 0x6F, 0x2A, 0x41, 0x47, 0x4C, 0xFE, 0xB8, 0x3B, 0xEC, 0x28, 0x94, 0x73, 0x0B, 0x9F, 0xAD, 0x26, 0x86, 0x72, 0x5D, 0xD2, 0x1C, 0x1A, 0xED, 40 | # 0xBF, 0xB7, 0x1E, 0x97, 0x9C, 0xB4, 0xA9, 0xE3, 0x48, 0x2B, 0xFF, 0xAB, 0x8F, 0xF4, 0x54, 0xC6, 0x51, 0x96, 0x62, 0x1F, 0xE5, 0xAA, 0x0A, 0xA2, 0x2E, 0x3A, 0x84, 0xFB, 0x81, 0xFC, 0xF3, 0x98, 41 | # 0xB5, 0xBD, 0x90, 0xDA, 0xF7, 0x85, 0x0D, 0xD3, 0xC5, 0x07, 0xB6, 0x3C, 0x17, 0xA3, 0x3D, 0x8E, 0x53, 0x19, 0xA7, 0x11, 0xD6, 0xF8, 0x52, 0x6B, 0xCA, 0xCC, 0xD9, 0xE4, 0x13, 0x70, 0x5A, 0xA5, 42 | # ] 43 | 44 | EncTable=[0 for i in range(256)] 45 | 46 | 47 | #鬼哭街 48 | hash0=0x1af59 49 | hash1=0xacc1 50 | 51 | 52 | 53 | ''' 54 | #Axanael 55 | hash0=0x18b61 56 | hash1=0x27697 57 | Thash=0x20101118 58 | ''' 59 | 60 | # #君と彼女と彼女の恋 61 | # hash0=0x1fd72 62 | # hash1=0x585d 63 | # Thash=0x12345678 64 | 65 | def InitEncTable(): 66 | for i in range(256): 67 | EncTable[DecTable[i]]=i 68 | 69 | ''' 70 | struct entry{ 71 | 0 name; 72 | 1 type; 73 | 2 dir_num; 74 | 3 data_off; 75 | 4 comprlen; 76 | 5 uncomprlen; 77 | 6 is_crypted; 78 | 7 is_compressed; 79 | 8 key; 80 | 9 dec_len; 81 | 10 hash0; 82 | 11 hash1; 83 | } 84 | ''' 85 | def EncryptEntry(entry,idx): 86 | thash=(entry[10]*entry[11])&0xffffffff 87 | for i in range(len(entry[0])): 88 | mi=(i*0xfc-(thash>>24)-(thash>>16)-(thash>>8)-thash-\ 89 | (idx>>24)-(idx>>16)-(idx>>8)-idx)&0xff 90 | t=(entry[0][i]-mi)&0xff 91 | entry[0][i]=t 92 | 93 | def Encrypt(entry,bf): 94 | thash=Thash 95 | for ch in entry[0]: 96 | thash-=ch 97 | entry[8]=(thash*len(entry[0]))&0xffffffff 98 | key=((entry[10]*entry[11]+entry[8])*entry[5])&0xffffffff 99 | 100 | if entry[4]>4096+len(entry[0]): 101 | entry[9]=4096+len(entry[0]) 102 | else: 103 | entry[9]=entry[4] 104 | 105 | for i in range(entry[9]): 106 | bf[i]=EncTable[(bf[i]+key+i)&0xff] 107 | 108 | #君と彼女と彼女の恋 109 | def Encrypt2(entry,bf): 110 | thash=Thash 111 | for ch in entry[0]: 112 | thash-=ch 113 | entry[8]=(thash*len(entry[0]))&0xffffffff 114 | key=((entry[10]*entry[11]+entry[8])*entry[5])&0xffffffff 115 | 116 | #int3() 117 | if entry[4]>4096+len(entry[0]): 118 | entry[9]=4096+len(entry[0]) 119 | else: 120 | entry[9]=entry[4] 121 | 122 | for i in range(entry[9]): 123 | temp=EncTable[(~(bf[i]+key+i))&0xff] 124 | temp=EncTable[temp] 125 | bf[i]=EncTable[temp] 126 | 127 | def packNpa(thedir,fname,is_compr,is_crypt): 128 | if not thedir.endswith('\\'): 129 | thedir+='\\' 130 | namepos=len(thedir) 131 | 132 | idxlen=0 133 | dircount=0 134 | filecount=0 135 | dirss=[] 136 | for root,dirs,files in os.walk(thedir): 137 | dirlen=len(root[namepos:].encode('932')) 138 | if dirlen>0: 139 | idxlen+=0x15+dirlen 140 | dirss.append(root[namepos:].encode('932')) 141 | dircount+=1 142 | else: 143 | dirlen=-1 144 | dirss.append('') 145 | for f in files: 146 | idxlen+=0x15+dirlen+1+len(f.encode('932')) 147 | filecount+=1 148 | 149 | fs=open(fname,'wb') 150 | idx=[] 151 | curpos=0 152 | curdir=0 153 | fs.seek(0x29+idxlen) 154 | for root,dirs,files in os.walk(thedir): 155 | dirlen=len(root[namepos:].encode('932')) 156 | if dirlen>0: 157 | rtdir=root[namepos:]+'\\' 158 | entry=[0 for i in range(12)] 159 | entry[0]=bytearray(root[namepos:].encode('932')) 160 | entry[1]=1 161 | if '\\' not in rtdir[0:-1]: 162 | entry[2]=0 163 | else: 164 | entry[2]=dirss.index(rtdir[0:-1].rsplit('\\',1)[0]) 165 | entry[3]=curpos 166 | entry[10]=hash0 167 | entry[11]=hash1 168 | idx.append(entry) 169 | curdir+=1 170 | else: 171 | rtdir=u"" 172 | for f in files: 173 | entry=[0 for i in range(12)] 174 | entry[0]=bytearray((rtdir+f).encode('932')) 175 | entry[1]=2 176 | entry[2]=curdir 177 | entry[3]=curpos 178 | infs=open(root+os.sep+f,'rb') 179 | entry[5]=os.path.getsize(root+os.sep+f) 180 | entry[6]=is_crypt 181 | entry[7]=is_compr 182 | entry[10]=hash0 183 | entry[11]=hash1 184 | if is_compr: 185 | stm=bytearray(zlib.compress(infs.read())) 186 | else: 187 | stm=bytearray(infs.read()) 188 | infs.close() 189 | entry[4]=len(stm) 190 | #if is_crypt: 191 | # Encrypt2(entry,stm) 192 | fs.write(stm) 193 | curpos+=len(stm) 194 | idx.append(entry) 195 | 196 | for i in range(len(idx)): 197 | EncryptEntry(idx[i],i) 198 | 199 | idxbf=bytearray() 200 | for entry in idx: 201 | idxbf+=pack('I',len(entry[0])) 202 | idxbf+=entry[0] 203 | idxbf+=pack(':Debug>") 4 | 5 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY $<1:${CMAKE_FINAL_OUTPUT_DIRECTORY}>) 6 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_FINAL_OUTPUT_DIRECTORY}>) 7 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_FINAL_OUTPUT_DIRECTORY}>) 8 | add_compile_options( 9 | /std:c++17 10 | /MP 11 | /wd4018 12 | /wd4819 13 | /wd4244 14 | /wd4267 15 | /DVERSION="${VERSION}" 16 | /DUNICODE 17 | /D_UNICODE 18 | ) 19 | project(patch) 20 | 21 | add_library(Horizontal MODULE patch.cpp patch.rc) 22 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../muramasa/patch/Detours-4.0.1/include) 23 | target_link_libraries(Horizontal ${CMAKE_CURRENT_SOURCE_DIR}/../../muramasa/patch/Detours-4.0.1/lib.X86/detours.lib) 24 | 25 | add_executable(HorizontalLoader WIN32 loader.cpp) 26 | target_link_libraries(HorizontalLoader ${CMAKE_CURRENT_SOURCE_DIR}/../../muramasa/patch/Detours-4.0.1/lib.X86/detours.lib) -------------------------------------------------------------------------------- /kikokugai/patch/loader.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow) 6 | { 7 | 8 | auto dllpath = std::filesystem::current_path() /"Horizontal.dll"; 9 | auto s=new char [dllpath.string().size()+1]; 10 | strcpy(s,dllpath.string().c_str()); 11 | 12 | STARTUPINFOW si; 13 | PROCESS_INFORMATION pi; 14 | ZeroMemory(&si, sizeof(STARTUPINFO)); 15 | ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); 16 | si.cb = sizeof(STARTUPINFO); 17 | 18 | wchar_t command[]=L".\\kikokugai_cn.exe"; 19 | DetourCreateProcessWithDllExW(NULL, command, NULL, 20 | NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, 21 | &si, &pi, s, NULL); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /kikokugai/patch/patch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | __declspec(dllexport) void dumy() {} 13 | 14 | HMODULE g_hm; 15 | std::string LoadResImage(LPCWSTR pszResID) 16 | { 17 | HMODULE hModule=g_hm; 18 | HRSRC hRsrc = ::FindResource (hModule, pszResID,L"DATA"); // type 19 | if (!hRsrc) 20 | return 0; 21 | // load resource into memory 22 | DWORD len = SizeofResource(hModule, hRsrc); 23 | BYTE* lpRsrc = (BYTE*)LoadResource(hModule, hRsrc); 24 | if (!lpRsrc) 25 | return 0; 26 | // Allocate global memory on which to create stream 27 | HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len); 28 | BYTE* pmem = (BYTE*)GlobalLock(m_hMem); 29 | memcpy(pmem,lpRsrc,len); 30 | 31 | GlobalUnlock(m_hMem); 32 | 33 | FreeResource(lpRsrc); 34 | return std::string((char*)pmem,len); 35 | } 36 | inline LPWSTR mbwc(LPCSTR lstr, UINT CodePage = CP_ACP) 37 | { 38 | int lsize = strlen(lstr); 39 | int wsize = (lsize + 1) << 1; 40 | auto wstr = new wchar_t[wsize]; 41 | auto n = MultiByteToWideChar(CodePage, 0, lstr, lsize, wstr, wsize); 42 | wstr[n] = L'\0'; 43 | return wstr; 44 | } 45 | HWND WINAPI HookCreateWindowExA( 46 | DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, 47 | int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) 48 | { 49 | auto Title=new char[strlen(lpWindowName)+100]; 50 | strcpy(Title,lpWindowName); 51 | strcat(Title," 横向文字补丁 by HIllya51"); 52 | return CreateWindowExW(dwExStyle,mbwc(lpClassName),mbwc(Title),dwStyle,X,Y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam);; 53 | } 54 | 55 | auto OriginalCreateWindowExA=CreateWindowExA; 56 | auto OriginalCreateFileA=CreateFileA; 57 | wchar_t temp_file[MAX_PATH]; 58 | HANDLE WINAPI HookCreateFileA( 59 | _In_ LPCSTR lpFileName, 60 | _In_ DWORD dwDesiredAccess, 61 | _In_ DWORD dwShareMode, 62 | _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, 63 | _In_ DWORD dwCreationDisposition, 64 | _In_ DWORD dwFlagsAndAttributes, 65 | _In_opt_ HANDLE hTemplateFile 66 | ) 67 | { 68 | if(strcmp(lpFileName,"nss-1.npa")==0||strstr(lpFileName,"\\nss-1.npa")){ 69 | return CreateFileW(temp_file,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile); 70 | } 71 | return OriginalCreateFileA(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile); 72 | } 73 | void AttachFunctions() 74 | { 75 | DetourAttach(&(PVOID&)OriginalCreateFileA, HookCreateFileA); 76 | DetourAttach(&(PVOID&)OriginalCreateWindowExA, HookCreateWindowExA); 77 | } 78 | 79 | BOOL APIENTRY DllMain( HMODULE hModule, 80 | DWORD ul_reason_for_call, 81 | LPVOID lpReserved 82 | ) 83 | { 84 | if (DetourIsHelperProcess()) { 85 | return TRUE; 86 | } 87 | switch (ul_reason_for_call) 88 | { 89 | case DLL_PROCESS_ATTACH: { 90 | g_hm=hModule; 91 | auto nssdata=LoadResImage(L"NSSNPA"); 92 | wchar_t temp_path[MAX_PATH]; 93 | GetTempPathW(MAX_PATH, temp_path); 94 | 95 | 96 | GetTempFileNameW(temp_path, L"nss-1.npa", 0, temp_file); 97 | 98 | FILE *f=_wfopen(temp_file,L"wb"); 99 | fwrite(nssdata.c_str(),1,nssdata.size(),f); 100 | fclose(f); 101 | 102 | DetourRestoreAfterWith(); 103 | DetourTransactionBegin(); 104 | DetourUpdateThread(GetCurrentThread()); 105 | 106 | AttachFunctions(); 107 | 108 | DetourTransactionCommit(); 109 | break; 110 | } 111 | case DLL_THREAD_ATTACH: 112 | break; 113 | case DLL_THREAD_DETACH: 114 | break; 115 | case DLL_PROCESS_DETACH: 116 | DeleteFile(temp_file); 117 | break; 118 | 119 | } 120 | return TRUE; 121 | } -------------------------------------------------------------------------------- /kikokugai/patch/patch.rc: -------------------------------------------------------------------------------- 1 | NSSNPA DATA "../nss-1.npa" -------------------------------------------------------------------------------- /kikokugai/solve.py: -------------------------------------------------------------------------------- 1 | with open('function.nss','rb') as ff: 2 | bs=ff.read() 3 | 4 | i1=bs.find(b'function SetText()') 5 | 6 | i2=bs.find(b'function SetTextBase()') 7 | 8 | bs=bs[:i1]+''' 9 | function SetText() 10 | { 11 | //TakeRateBack();//写真撮る用 12 | $WindXSize=1024; 13 | $WindYSize=768; 14 | 15 | if($OnFW){ 16 | $LoadTextWsize = 540; 17 | $LoadTextHsize = 150; 18 | 19 | $TypeBeginFwX=145;//変更 20 | $TypeBeginFwY=567; 21 | $TypeBeginNwX=145;//変更 22 | $TypeBeginNwY=567; 23 | if($FwType=="fw"){ 24 | $TypeBeginFaceX=$TypeBeginFwX; 25 | $TypeBeginFaceY=$TypeBeginFwY; 26 | }else{ 27 | $TypeBeginFaceX=$TypeBeginNwX; 28 | $TypeBeginFaceY=$TypeBeginNwY; 29 | } 30 | $TypeBeginBoxX=@0; 31 | $TypeBeginBoxY=@0; 32 | $TypeBeginTextX=320; 33 | $TypeBeginTextY=572; 34 | $TypeBeginIconX=830; 35 | $TypeBeginIconY=685; 36 | 37 | $TypeBeginNut="@boxHF"; 38 | }else{ 39 | $LoadTextWsize = 660; 40 | $LoadTextHsize = 150; 41 | 42 | $TypeBeginBoxX=@0; 43 | $TypeBeginBoxY=@0; 44 | $TypeBeginTextX=185; 45 | $TypeBeginTextY=570; 46 | $TypeBeginIconX=800; 47 | $TypeBeginIconY=680; 48 | 49 | $TypeBeginNut="@boxHN"; 50 | } 51 | 52 | 53 | SetTextBase2(); 54 | } 55 | 56 | function SetTextL() 57 | { 58 | //TakeRateBack();//写真撮る用 59 | $WindXSize=1024; 60 | $WindYSize=768; 61 | 62 | if($OnFW){ 63 | $LoadTextWsize = 540; 64 | $LoadTextHsize = 150; 65 | 66 | 67 | $TypeBeginFwX=145;//変更 68 | $TypeBeginFwY=567; 69 | $TypeBeginNwX=145;//変更 70 | $TypeBeginNwY=567; 71 | if($FwType=="fw"){ 72 | $TypeBeginFaceX=$TypeBeginFwX; 73 | $TypeBeginFaceY=$TypeBeginFwY; 74 | }else{ 75 | $TypeBeginFaceX=$TypeBeginNwX; 76 | $TypeBeginFaceY=$TypeBeginNwY; 77 | } 78 | 79 | 80 | $TypeBeginBoxX=@0; 81 | $TypeBeginBoxY=@0; 82 | $TypeBeginTextX=320; 83 | $TypeBeginTextY=572; 84 | $TypeBeginIconX=830; 85 | $TypeBeginIconY=685; 86 | 87 | $TypeBeginNut="@boxHF"; 88 | }else{ 89 | $LoadTextWsize = 660; 90 | $LoadTextHsize = 150; 91 | 92 | $TypeBeginBoxX=@0; 93 | $TypeBeginBoxY=@0; 94 | $TypeBeginTextX=185; 95 | $TypeBeginTextY=570; 96 | $TypeBeginIconX=800; 97 | $TypeBeginIconY=680; 98 | 99 | $TypeBeginNut="@boxHN"; 100 | } 101 | 102 | SetTextBase2(); 103 | } 104 | 105 | 106 | function SetTextR() 107 | { 108 | //TakeRateBack();//写真撮る用 109 | $WindXSize=1024; 110 | $WindYSize=768; 111 | 112 | if($OnFW){ 113 | $LoadTextWsize = 540; 114 | $LoadTextHsize = 150; 115 | 116 | 117 | $TypeBeginFwX=145;//変更 118 | $TypeBeginFwY=567; 119 | $TypeBeginNwX=145;//変更 120 | $TypeBeginNwY=567; 121 | if($FwType=="fw"){ 122 | $TypeBeginFaceX=$TypeBeginFwX; 123 | $TypeBeginFaceY=$TypeBeginFwY; 124 | }else{ 125 | $TypeBeginFaceX=$TypeBeginNwX; 126 | $TypeBeginFaceY=$TypeBeginNwY; 127 | } 128 | 129 | 130 | $TypeBeginBoxX=@0; 131 | $TypeBeginBoxY=@0; 132 | $TypeBeginTextX=320; 133 | $TypeBeginTextY=572; 134 | $TypeBeginIconX=830; 135 | $TypeBeginIconY=685; 136 | 137 | $TypeBeginNut="@boxHF"; 138 | }else{ 139 | $LoadTextWsize = 660; 140 | $LoadTextHsize = 150; 141 | 142 | $TypeBeginBoxX=@0; 143 | $TypeBeginBoxY=@0; 144 | $TypeBeginTextX=185; 145 | $TypeBeginTextY=570; 146 | $TypeBeginIconX=800; 147 | $TypeBeginIconY=680; 148 | 149 | $TypeBeginNut="@boxHN"; 150 | } 151 | 152 | 153 | SetTextBase2(); 154 | } 155 | 156 | 157 | 158 | '''.encode('cp932')+bs[i2:] 159 | 160 | with open('nss-1-h/function.nss','wb') as ff: 161 | ff.write(bs) 162 | -------------------------------------------------------------------------------- /muramasa/patch/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iosfwd": "cpp", 4 | "istream": "cpp", 5 | "atomic": "cpp", 6 | "bit": "cpp", 7 | "cctype": "cpp", 8 | "clocale": "cpp", 9 | "cmath": "cpp", 10 | "compare": "cpp", 11 | "concepts": "cpp", 12 | "cstddef": "cpp", 13 | "cstdint": "cpp", 14 | "cstdio": "cpp", 15 | "cstdlib": "cpp", 16 | "cstring": "cpp", 17 | "ctime": "cpp", 18 | "cwchar": "cpp", 19 | "exception": "cpp", 20 | "fstream": "cpp", 21 | "initializer_list": "cpp", 22 | "ios": "cpp", 23 | "iostream": "cpp", 24 | "iterator": "cpp", 25 | "limits": "cpp", 26 | "memory": "cpp", 27 | "new": "cpp", 28 | "ostream": "cpp", 29 | "stdexcept": "cpp", 30 | "streambuf": "cpp", 31 | "system_error": "cpp", 32 | "tuple": "cpp", 33 | "type_traits": "cpp", 34 | "typeinfo": "cpp", 35 | "utility": "cpp", 36 | "xfacet": "cpp", 37 | "xiosbase": "cpp", 38 | "xlocale": "cpp", 39 | "xlocinfo": "cpp", 40 | "xlocnum": "cpp", 41 | "xmemory": "cpp", 42 | "xstddef": "cpp", 43 | "xstring": "cpp", 44 | "xtr1common": "cpp", 45 | "xutility": "cpp", 46 | "string": "cpp", 47 | "vector": "cpp", 48 | "algorithm": "cpp", 49 | "array": "cpp", 50 | "charconv": "cpp", 51 | "chrono": "cpp", 52 | "deque": "cpp", 53 | "filesystem": "cpp", 54 | "format": "cpp", 55 | "forward_list": "cpp", 56 | "functional": "cpp", 57 | "iomanip": "cpp", 58 | "list": "cpp", 59 | "locale": "cpp", 60 | "mutex": "cpp", 61 | "optional": "cpp", 62 | "ratio": "cpp", 63 | "regex": "cpp", 64 | "sstream": "cpp", 65 | "stop_token": "cpp", 66 | "thread": "cpp", 67 | "unordered_map": "cpp", 68 | "unordered_set": "cpp", 69 | "xhash": "cpp", 70 | "xlocbuf": "cpp", 71 | "xlocmes": "cpp", 72 | "xlocmon": "cpp", 73 | "xloctime": "cpp", 74 | "xtree": "cpp", 75 | "xmemory0": "cpp", 76 | "map": "cpp", 77 | "any": "cpp", 78 | "numeric": "cpp", 79 | "ranges": "cpp", 80 | "span": "cpp", 81 | "valarray": "cpp", 82 | "codecvt": "cpp", 83 | "string_view": "cpp", 84 | "xthread": "cpp", 85 | "queue": "cpp", 86 | "variant": "cpp" 87 | } 88 | } -------------------------------------------------------------------------------- /muramasa/patch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | 3 | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") 4 | 5 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY $<1:${CMAKE_FINAL_OUTPUT_DIRECTORY}>) 6 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_FINAL_OUTPUT_DIRECTORY}>) 7 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_FINAL_OUTPUT_DIRECTORY}>) 8 | add_compile_options( 9 | /std:c++17 10 | /MP 11 | /wd4018 12 | /wd4819 13 | /wd4244 14 | /wd4267 15 | /DVERSION="${VERSION}" 16 | /DUNICODE 17 | /D_UNICODE 18 | ) 19 | project(patch) 20 | 21 | add_library(Horizontal MODULE patch.cpp patch.rc) 22 | include_directories(Detours-4.0.1/include) 23 | target_link_libraries(Horizontal ${CMAKE_CURRENT_SOURCE_DIR}/Detours-4.0.1/lib.X86/detours.lib) 24 | 25 | add_executable(HorizontalLoader WIN32 loader.cpp) 26 | target_link_libraries(HorizontalLoader ${CMAKE_CURRENT_SOURCE_DIR}/Detours-4.0.1/lib.X86/detours.lib) -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/CREDITS.TXT: -------------------------------------------------------------------------------- 1 | ============================================================================== 2 | The following individuals have helped identify specific bugs and improvements 3 | in Detours. The entire Detours community has benefited from their help. 4 | ============================================================================== 5 | 6 | * Jay Krell: Identified issue with VirtualSize == 0 files created in 7 | NT 3.1 images. (Build_339) 8 | 9 | * Igor Odnovorov: Identified an issue with the placement of the trampoline 10 | region when a function is detoured twice and the second 11 | trampoline region is outside of the +/- 2GB range of 12 | the target. (Build_337) 13 | 14 | * Jay Krell: Identified need for some programs to enumerate the 15 | address of IAT entries. (Build_336) 16 | 17 | * Calvin Hsia: Identified need for some program to change the excluded 18 | system region. (Build_336) 19 | 20 | * Adam Smith: Identified error in failure handling when VirtualProect 21 | cannot make pages executable because the Prohibit 22 | Dynamic Code Generation mitigation policy has been 23 | applied to a process. (Build_335) 24 | 25 | * Ben Faull: Identified fix to detour_alloc_region_from_lo and 26 | detour_alloc_region_from_hi that preserves ASLR entropy. 27 | (Build_334) 28 | 29 | * Shaoxiang Su: Reported errors building with Visual Studio 2015. 30 | (Build_332) 31 | 32 | * Jay Krell: Identified and resolved significant gaps in the X86, X64 33 | and IA64 disassemblers for instruction found in code, 34 | but seldom found in function prologues. (Build_331) 35 | 36 | * Allan Murphy: Identify error in rep and jmp ds: encodings. (Build_331) 37 | 38 | * Philip Bacon: Identified incorrect entry point return for pure 39 | resource-only binaries. (Build_330) 40 | 41 | * Jay Krell: Identified failure in DetourAttachEx to update nAlign. 42 | (Build_330) 43 | 44 | * Sumit Sarin: Helped debug error with packed binaries. 45 | (Build_329) 46 | 47 | * Nitya Kumar Sharma: Reported bug in DetourAfterWithDll for 32/64 agnostic 48 | EXEs. 49 | (Build_327) 50 | 51 | * Richard Black: Identified a large number of typos in documentation. 52 | (Build_326) 53 | 54 | * Michael Bilodeau: Identified bug in DetourUpdateProcessWithDll when the 55 | target process contains a Detours payload *after* all 56 | valid PE binaries. 57 | (Build_324) 58 | 59 | * Meera Jindal: Reported bug in identification of target address in 60 | DetourCopyInstruction for jmp[] and call[] on x86 & x64, 61 | the ff15 and ff25 opcodes. 62 | (Build_323) 63 | 64 | * Ken Johnson: Assistance with SAL 2.0 annotations. 65 | (Build_319) 66 | 67 | * Nick Wood: Identified bug in DetourFindFunction on ARM. 68 | (Build_314) 69 | 70 | * Mark Russinovich: Helped debug DetourCreateProcessWithDllEx. 71 | (Build_314) 72 | 73 | * John Lin: Implementation idea for DetoursCreateProcessWithDllEx. 74 | (Build_314) 75 | 76 | * Andrew Zawadowskiy Reported an improper memory page permissions 77 | vulnerability in Detours 2.1. (Vulnerability does not 78 | exist in versions later than Detours 2.1.) 79 | (Build_223) 80 | 81 | * Nightxie: Identified bug in detour_alloc_round_up_to_region. 82 | (Build_310) 83 | 84 | * Diana Milirud: Identified bug in B* instructions on ARM. 85 | (Build_309) 86 | 87 | * Juan Carlos Identified correct MSIL entry point for unsigned MSIL. 88 | Luciani: (Build_308) 89 | 90 | * Lee Hunt Suggested improvements in algorithm for allocation of 91 | Lawrence Landauer trampoline regions on x64 to avoid collisions with 92 | Joe Laughlin: system DLLs. 93 | (Build_307) 94 | 95 | * Tyler Sims Identified bug in handling of "anycpu" MSIL binaries 96 | Darren Kennedy: on x64. 97 | (Build_307) 98 | 99 | * Andre Vachon: Help with optimized binaries. 100 | (Build 301) 101 | 102 | * Chris Mann: Identified fix not forward ported from 2.2 to 3.0. 103 | (Build_301) 104 | 105 | * Mark Irving: Identified bug with EXEs missing second import table. 106 | (Build_300) 107 | 108 | * Ben Schwarz: Identified bug in handling of multi-byte NOPs. 109 | (Build_300) 110 | 111 | * Aaron Giles Coded initial ARM/Thumb2 disassembler. 112 | Jared Henderson: (Build_300) 113 | 114 | * Doug Brubacher: Coded initial x86 disassembler. 115 | (Build_100) 116 | -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/LICENSE.md: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation 2 | 3 | All rights reserved. 4 | 5 | # MIT License 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of 8 | this software and associated documentation files (the "Software"), to deal in 9 | the Software without restriction, including without limitation the rights to 10 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 11 | of the Software, and to permit persons to whom the Software is furnished to do 12 | so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/Makefile: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | ## 3 | ## Makefile for Detours. 4 | ## 5 | ## Microsoft Research Detours Package 6 | ## 7 | ## Copyright (c) Microsoft Corporation. All rights reserved. 8 | ## 9 | 10 | ROOT = . 11 | !include "$(ROOT)\system.mak" 12 | 13 | all: 14 | cd "$(MAKEDIR)" 15 | @if exist "$(MAKEDIR)\core\makefile" cd "$(MAKEDIR)\core" && $(MAKE) /NOLOGO /$(MAKEFLAGS) 16 | cd "$(MAKEDIR)\src" 17 | @$(MAKE) /NOLOGO /$(MAKEFLAGS) 18 | cd "$(MAKEDIR)\samples" 19 | @$(MAKE) /NOLOGO /$(MAKEFLAGS) 20 | @if exist "$(MAKEDIR)\bugs\makefile" cd "$(MAKEDIR)\bugs" && $(MAKE) /NOLOGO /$(MAKEFLAGS) 21 | cd "$(MAKEDIR)" 22 | 23 | clean: 24 | cd "$(MAKEDIR)" 25 | @if exist "$(MAKEDIR)\core\makefile" cd "$(MAKEDIR)\core" && $(MAKE) /NOLOGO /$(MAKEFLAGS) clean 26 | cd "$(MAKEDIR)\src" 27 | @$(MAKE) /NOLOGO /$(MAKEFLAGS) clean 28 | cd "$(MAKEDIR)\samples" 29 | @$(MAKE) /NOLOGO /$(MAKEFLAGS) clean 30 | @if exist "$(MAKEDIR)\bugs\makefile" cd "$(MAKEDIR)\bugs" && $(MAKE) /NOLOGO /$(MAKEFLAGS) clean 31 | cd "$(MAKEDIR)" 32 | 33 | realclean: clean 34 | cd "$(MAKEDIR)" 35 | @if exist "$(MAKEDIR)\core\makefile" cd "$(MAKEDIR)\core" && $(MAKE) /NOLOGO /$(MAKEFLAGS) realclean 36 | cd "$(MAKEDIR)\src" 37 | @$(MAKE) /NOLOGO /$(MAKEFLAGS) realclean 38 | cd "$(MAKEDIR)\samples" 39 | @$(MAKE) /NOLOGO /$(MAKEFLAGS) realclean 40 | @if exist "$(MAKEDIR)\bugs\makefile" cd "$(MAKEDIR)\bugs" && $(MAKE) /NOLOGO /$(MAKEFLAGS) realclean 41 | cd "$(MAKEDIR)" 42 | -rmdir /q /s $(INCDS) 2> nul 43 | -rmdir /q /s $(LIBDS) 2> nul 44 | -rmdir /q /s $(BINDS) 2> nul 45 | -rmdir /q /s dist 2> nul 46 | -del docsrc\detours.chm 2> nul 47 | -del /q *.msi 2>nul 48 | -del /q /f /s *~ 2>nul 49 | 50 | test: 51 | cd "$(MAKEDIR)\samples" 52 | @$(MAKE) /NOLOGO /$(MAKEFLAGS) test 53 | cd "$(MAKEDIR)" 54 | 55 | ################################################################# End of File. 56 | -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/README.md: -------------------------------------------------------------------------------- 1 | # Microsoft Research Detours Package 2 | 3 | Detours is a software package for monitoring and instrumenting API calls on Windows. Detours 4 | has been used by many ISVs and is also used by product teams at Microsoft. Detours is now available under 5 | a standard open source license (MIT). This simplifies licensing for programmers using Detours 6 | and allows the community to support Detours using open source tools and processes. 7 | 8 | Detours is compatible with the Windows NT family of 9 | operating systems: Windows NT, Windows XP, Windows Server 2003, Windows 7, 10 | Windows 8, and Windows 10. It cannot be used by Window Store apps 11 | because Detours requires APIs not available to those applications. 12 | This repo contains the source code for version 4.0.1 of Detours. 13 | 14 | For technical documentation on Detours, see the [Detours Wiki](https://github.com/microsoft/Detours/wiki). 15 | For directions on how to build and run samples, see the 16 | samples [README.txt](https://github.com/Microsoft/Detours/blob/master/samples/README.TXT) file. 17 | 18 | ## Contributing 19 | 20 | The [`Detours`](https://github.com/microsoft/detours) repository is where development is done. 21 | Here are some ways you can participate in the project: 22 | 23 | * [Answer questions](https://github.com/microsoft/detours/issues) about using Detours. 24 | * [Improve the Wiki](https://github.com/microsoft/detours/Wiki). 25 | * [Submit bugs](https://github.com/microsoft/detours/issues) and help us verify fixes and changes as they are checked in. 26 | * Review [source code changes](https://github.com/microsoft/detours/pulls). 27 | 28 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. 29 | 30 | ## Issues, questions, and feedback 31 | 32 | * Open an issue on [GitHub Issues](https://github.com/Microsoft/detours/issues). 33 | 34 | ## License 35 | 36 | Copyright (c) Microsoft Corporation. All rights reserved. 37 | 38 | Licensed under the [MIT](LICENSE.txt) License. 39 | -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/include/detours.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Core Detours Functionality (detours.h of detours.lib) 4 | // 5 | // Microsoft Research Detours Package, Version 4.0.1 6 | // 7 | // Copyright (c) Microsoft Corporation. All rights reserved. 8 | // 9 | 10 | #pragma once 11 | #ifndef _DETOURS_H_ 12 | #define _DETOURS_H_ 13 | 14 | #define DETOURS_VERSION 0x4c0c1 // 0xMAJORcMINORcPATCH 15 | 16 | ////////////////////////////////////////////////////////////////////////////// 17 | // 18 | 19 | #undef DETOURS_X64 20 | #undef DETOURS_X86 21 | #undef DETOURS_IA64 22 | #undef DETOURS_ARM 23 | #undef DETOURS_ARM64 24 | #undef DETOURS_BITS 25 | #undef DETOURS_32BIT 26 | #undef DETOURS_64BIT 27 | 28 | #if defined(_X86_) 29 | #define DETOURS_X86 30 | #define DETOURS_OPTION_BITS 64 31 | 32 | #elif defined(_AMD64_) 33 | #define DETOURS_X64 34 | #define DETOURS_OPTION_BITS 32 35 | 36 | #elif defined(_IA64_) 37 | #define DETOURS_IA64 38 | #define DETOURS_OPTION_BITS 32 39 | 40 | #elif defined(_ARM_) 41 | #define DETOURS_ARM 42 | 43 | #elif defined(_ARM64_) 44 | #define DETOURS_ARM64 45 | 46 | #else 47 | #error Unknown architecture (x86, amd64, ia64, arm, arm64) 48 | #endif 49 | 50 | #ifdef _WIN64 51 | #undef DETOURS_32BIT 52 | #define DETOURS_64BIT 1 53 | #define DETOURS_BITS 64 54 | // If all 64bit kernels can run one and only one 32bit architecture. 55 | //#define DETOURS_OPTION_BITS 32 56 | #else 57 | #define DETOURS_32BIT 1 58 | #undef DETOURS_64BIT 59 | #define DETOURS_BITS 32 60 | // If all 64bit kernels can run one and only one 32bit architecture. 61 | //#define DETOURS_OPTION_BITS 32 62 | #endif 63 | 64 | #define VER_DETOURS_BITS DETOUR_STRINGIFY(DETOURS_BITS) 65 | 66 | ////////////////////////////////////////////////////////////////////////////// 67 | // 68 | 69 | #if (_MSC_VER < 1299) 70 | typedef LONG LONG_PTR; 71 | typedef ULONG ULONG_PTR; 72 | #endif 73 | 74 | ///////////////////////////////////////////////// SAL 2.0 Annotations w/o SAL. 75 | // 76 | // These definitions are include so that Detours will build even if the 77 | // compiler doesn't have full SAL 2.0 support. 78 | // 79 | #ifndef DETOURS_DONT_REMOVE_SAL_20 80 | 81 | #ifdef DETOURS_TEST_REMOVE_SAL_20 82 | #undef _Analysis_assume_ 83 | #undef _Benign_race_begin_ 84 | #undef _Benign_race_end_ 85 | #undef _Field_range_ 86 | #undef _Field_size_ 87 | #undef _In_ 88 | #undef _In_bytecount_ 89 | #undef _In_count_ 90 | #undef _In_opt_ 91 | #undef _In_opt_bytecount_ 92 | #undef _In_opt_count_ 93 | #undef _In_opt_z_ 94 | #undef _In_range_ 95 | #undef _In_reads_ 96 | #undef _In_reads_bytes_ 97 | #undef _In_reads_opt_ 98 | #undef _In_reads_opt_bytes_ 99 | #undef _In_reads_or_z_ 100 | #undef _In_z_ 101 | #undef _Inout_ 102 | #undef _Inout_opt_ 103 | #undef _Inout_z_count_ 104 | #undef _Out_ 105 | #undef _Out_opt_ 106 | #undef _Out_writes_ 107 | #undef _Outptr_result_maybenull_ 108 | #undef _Readable_bytes_ 109 | #undef _Success_ 110 | #undef _Writable_bytes_ 111 | #undef _Pre_notnull_ 112 | #endif 113 | 114 | #if defined(_Deref_out_opt_z_) && !defined(_Outptr_result_maybenull_) 115 | #define _Outptr_result_maybenull_ _Deref_out_opt_z_ 116 | #endif 117 | 118 | #if defined(_In_count_) && !defined(_In_reads_) 119 | #define _In_reads_(x) _In_count_(x) 120 | #endif 121 | 122 | #if defined(_In_opt_count_) && !defined(_In_reads_opt_) 123 | #define _In_reads_opt_(x) _In_opt_count_(x) 124 | #endif 125 | 126 | #if defined(_In_opt_bytecount_) && !defined(_In_reads_opt_bytes_) 127 | #define _In_reads_opt_bytes_(x) _In_opt_bytecount_(x) 128 | #endif 129 | 130 | #if defined(_In_bytecount_) && !defined(_In_reads_bytes_) 131 | #define _In_reads_bytes_(x) _In_bytecount_(x) 132 | #endif 133 | 134 | #ifndef _In_ 135 | #define _In_ 136 | #endif 137 | 138 | #ifndef _In_bytecount_ 139 | #define _In_bytecount_(x) 140 | #endif 141 | 142 | #ifndef _In_count_ 143 | #define _In_count_(x) 144 | #endif 145 | 146 | #ifndef _In_opt_ 147 | #define _In_opt_ 148 | #endif 149 | 150 | #ifndef _In_opt_bytecount_ 151 | #define _In_opt_bytecount_(x) 152 | #endif 153 | 154 | #ifndef _In_opt_count_ 155 | #define _In_opt_count_(x) 156 | #endif 157 | 158 | #ifndef _In_opt_z_ 159 | #define _In_opt_z_ 160 | #endif 161 | 162 | #ifndef _In_range_ 163 | #define _In_range_(x,y) 164 | #endif 165 | 166 | #ifndef _In_reads_ 167 | #define _In_reads_(x) 168 | #endif 169 | 170 | #ifndef _In_reads_bytes_ 171 | #define _In_reads_bytes_(x) 172 | #endif 173 | 174 | #ifndef _In_reads_opt_ 175 | #define _In_reads_opt_(x) 176 | #endif 177 | 178 | #ifndef _In_reads_opt_bytes_ 179 | #define _In_reads_opt_bytes_(x) 180 | #endif 181 | 182 | #ifndef _In_reads_or_z_ 183 | #define _In_reads_or_z_ 184 | #endif 185 | 186 | #ifndef _In_z_ 187 | #define _In_z_ 188 | #endif 189 | 190 | #ifndef _Inout_ 191 | #define _Inout_ 192 | #endif 193 | 194 | #ifndef _Inout_opt_ 195 | #define _Inout_opt_ 196 | #endif 197 | 198 | #ifndef _Inout_z_count_ 199 | #define _Inout_z_count_(x) 200 | #endif 201 | 202 | #ifndef _Out_ 203 | #define _Out_ 204 | #endif 205 | 206 | #ifndef _Out_opt_ 207 | #define _Out_opt_ 208 | #endif 209 | 210 | #ifndef _Out_writes_ 211 | #define _Out_writes_(x) 212 | #endif 213 | 214 | #ifndef _Outptr_result_maybenull_ 215 | #define _Outptr_result_maybenull_ 216 | #endif 217 | 218 | #ifndef _Writable_bytes_ 219 | #define _Writable_bytes_(x) 220 | #endif 221 | 222 | #ifndef _Readable_bytes_ 223 | #define _Readable_bytes_(x) 224 | #endif 225 | 226 | #ifndef _Success_ 227 | #define _Success_(x) 228 | #endif 229 | 230 | #ifndef _Pre_notnull_ 231 | #define _Pre_notnull_ 232 | #endif 233 | 234 | #ifdef DETOURS_INTERNAL 235 | 236 | #pragma warning(disable:4615) // unknown warning type (suppress with older compilers) 237 | 238 | #ifndef _Benign_race_begin_ 239 | #define _Benign_race_begin_ 240 | #endif 241 | 242 | #ifndef _Benign_race_end_ 243 | #define _Benign_race_end_ 244 | #endif 245 | 246 | #ifndef _Field_size_ 247 | #define _Field_size_(x) 248 | #endif 249 | 250 | #ifndef _Field_range_ 251 | #define _Field_range_(x,y) 252 | #endif 253 | 254 | #ifndef _Analysis_assume_ 255 | #define _Analysis_assume_(x) 256 | #endif 257 | 258 | #endif // DETOURS_INTERNAL 259 | #endif // DETOURS_DONT_REMOVE_SAL_20 260 | 261 | ////////////////////////////////////////////////////////////////////////////// 262 | // 263 | #ifndef GUID_DEFINED 264 | #define GUID_DEFINED 265 | typedef struct _GUID 266 | { 267 | DWORD Data1; 268 | WORD Data2; 269 | WORD Data3; 270 | BYTE Data4[ 8 ]; 271 | } GUID; 272 | 273 | #ifdef INITGUID 274 | #define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ 275 | const GUID name \ 276 | = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } 277 | #else 278 | #define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ 279 | const GUID name 280 | #endif // INITGUID 281 | #endif // !GUID_DEFINED 282 | 283 | #if defined(__cplusplus) 284 | #ifndef _REFGUID_DEFINED 285 | #define _REFGUID_DEFINED 286 | #define REFGUID const GUID & 287 | #endif // !_REFGUID_DEFINED 288 | #else // !__cplusplus 289 | #ifndef _REFGUID_DEFINED 290 | #define _REFGUID_DEFINED 291 | #define REFGUID const GUID * const 292 | #endif // !_REFGUID_DEFINED 293 | #endif // !__cplusplus 294 | 295 | #ifndef ARRAYSIZE 296 | #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0])) 297 | #endif 298 | 299 | // 300 | ////////////////////////////////////////////////////////////////////////////// 301 | 302 | #ifdef __cplusplus 303 | extern "C" { 304 | #endif // __cplusplus 305 | 306 | /////////////////////////////////////////////////// Instruction Target Macros. 307 | // 308 | #define DETOUR_INSTRUCTION_TARGET_NONE ((PVOID)0) 309 | #define DETOUR_INSTRUCTION_TARGET_DYNAMIC ((PVOID)(LONG_PTR)-1) 310 | #define DETOUR_SECTION_HEADER_SIGNATURE 0x00727444 // "Dtr\0" 311 | 312 | extern const GUID DETOUR_EXE_RESTORE_GUID; 313 | extern const GUID DETOUR_EXE_HELPER_GUID; 314 | 315 | #define DETOUR_TRAMPOLINE_SIGNATURE 0x21727444 // Dtr! 316 | typedef struct _DETOUR_TRAMPOLINE DETOUR_TRAMPOLINE, *PDETOUR_TRAMPOLINE; 317 | 318 | /////////////////////////////////////////////////////////// Binary Structures. 319 | // 320 | #pragma pack(push, 8) 321 | typedef struct _DETOUR_SECTION_HEADER 322 | { 323 | DWORD cbHeaderSize; 324 | DWORD nSignature; 325 | DWORD nDataOffset; 326 | DWORD cbDataSize; 327 | 328 | DWORD nOriginalImportVirtualAddress; 329 | DWORD nOriginalImportSize; 330 | DWORD nOriginalBoundImportVirtualAddress; 331 | DWORD nOriginalBoundImportSize; 332 | 333 | DWORD nOriginalIatVirtualAddress; 334 | DWORD nOriginalIatSize; 335 | DWORD nOriginalSizeOfImage; 336 | DWORD cbPrePE; 337 | 338 | DWORD nOriginalClrFlags; 339 | DWORD reserved1; 340 | DWORD reserved2; 341 | DWORD reserved3; 342 | 343 | // Followed by cbPrePE bytes of data. 344 | } DETOUR_SECTION_HEADER, *PDETOUR_SECTION_HEADER; 345 | 346 | typedef struct _DETOUR_SECTION_RECORD 347 | { 348 | DWORD cbBytes; 349 | DWORD nReserved; 350 | GUID guid; 351 | } DETOUR_SECTION_RECORD, *PDETOUR_SECTION_RECORD; 352 | 353 | typedef struct _DETOUR_CLR_HEADER 354 | { 355 | // Header versioning 356 | ULONG cb; 357 | USHORT MajorRuntimeVersion; 358 | USHORT MinorRuntimeVersion; 359 | 360 | // Symbol table and startup information 361 | IMAGE_DATA_DIRECTORY MetaData; 362 | ULONG Flags; 363 | 364 | // Followed by the rest of the IMAGE_COR20_HEADER 365 | } DETOUR_CLR_HEADER, *PDETOUR_CLR_HEADER; 366 | 367 | typedef struct _DETOUR_EXE_RESTORE 368 | { 369 | DWORD cb; 370 | DWORD cbidh; 371 | DWORD cbinh; 372 | DWORD cbclr; 373 | 374 | PBYTE pidh; 375 | PBYTE pinh; 376 | PBYTE pclr; 377 | 378 | IMAGE_DOS_HEADER idh; 379 | union { 380 | IMAGE_NT_HEADERS inh; 381 | IMAGE_NT_HEADERS32 inh32; 382 | IMAGE_NT_HEADERS64 inh64; 383 | BYTE raw[sizeof(IMAGE_NT_HEADERS64) + 384 | sizeof(IMAGE_SECTION_HEADER) * 32]; 385 | }; 386 | DETOUR_CLR_HEADER clr; 387 | 388 | } DETOUR_EXE_RESTORE, *PDETOUR_EXE_RESTORE; 389 | 390 | typedef struct _DETOUR_EXE_HELPER 391 | { 392 | DWORD cb; 393 | DWORD pid; 394 | DWORD nDlls; 395 | CHAR rDlls[4]; 396 | } DETOUR_EXE_HELPER, *PDETOUR_EXE_HELPER; 397 | 398 | #pragma pack(pop) 399 | 400 | #define DETOUR_SECTION_HEADER_DECLARE(cbSectionSize) \ 401 | { \ 402 | sizeof(DETOUR_SECTION_HEADER),\ 403 | DETOUR_SECTION_HEADER_SIGNATURE,\ 404 | sizeof(DETOUR_SECTION_HEADER),\ 405 | (cbSectionSize),\ 406 | \ 407 | 0,\ 408 | 0,\ 409 | 0,\ 410 | 0,\ 411 | \ 412 | 0,\ 413 | 0,\ 414 | 0,\ 415 | 0,\ 416 | } 417 | 418 | /////////////////////////////////////////////////////////////// Helper Macros. 419 | // 420 | #define DETOURS_STRINGIFY(x) DETOURS_STRINGIFY_(x) 421 | #define DETOURS_STRINGIFY_(x) #x 422 | 423 | ///////////////////////////////////////////////////////////// Binary Typedefs. 424 | // 425 | typedef BOOL (CALLBACK *PF_DETOUR_BINARY_BYWAY_CALLBACK)( 426 | _In_opt_ PVOID pContext, 427 | _In_opt_ LPCSTR pszFile, 428 | _Outptr_result_maybenull_ LPCSTR *ppszOutFile); 429 | 430 | typedef BOOL (CALLBACK *PF_DETOUR_BINARY_FILE_CALLBACK)( 431 | _In_opt_ PVOID pContext, 432 | _In_ LPCSTR pszOrigFile, 433 | _In_ LPCSTR pszFile, 434 | _Outptr_result_maybenull_ LPCSTR *ppszOutFile); 435 | 436 | typedef BOOL (CALLBACK *PF_DETOUR_BINARY_SYMBOL_CALLBACK)( 437 | _In_opt_ PVOID pContext, 438 | _In_ ULONG nOrigOrdinal, 439 | _In_ ULONG nOrdinal, 440 | _Out_ ULONG *pnOutOrdinal, 441 | _In_opt_ LPCSTR pszOrigSymbol, 442 | _In_opt_ LPCSTR pszSymbol, 443 | _Outptr_result_maybenull_ LPCSTR *ppszOutSymbol); 444 | 445 | typedef BOOL (CALLBACK *PF_DETOUR_BINARY_COMMIT_CALLBACK)( 446 | _In_opt_ PVOID pContext); 447 | 448 | typedef BOOL (CALLBACK *PF_DETOUR_ENUMERATE_EXPORT_CALLBACK)(_In_opt_ PVOID pContext, 449 | _In_ ULONG nOrdinal, 450 | _In_opt_ LPCSTR pszName, 451 | _In_opt_ PVOID pCode); 452 | 453 | typedef BOOL (CALLBACK *PF_DETOUR_IMPORT_FILE_CALLBACK)(_In_opt_ PVOID pContext, 454 | _In_opt_ HMODULE hModule, 455 | _In_opt_ LPCSTR pszFile); 456 | 457 | typedef BOOL (CALLBACK *PF_DETOUR_IMPORT_FUNC_CALLBACK)(_In_opt_ PVOID pContext, 458 | _In_ DWORD nOrdinal, 459 | _In_opt_ LPCSTR pszFunc, 460 | _In_opt_ PVOID pvFunc); 461 | 462 | // Same as PF_DETOUR_IMPORT_FUNC_CALLBACK but extra indirection on last parameter. 463 | typedef BOOL (CALLBACK *PF_DETOUR_IMPORT_FUNC_CALLBACK_EX)(_In_opt_ PVOID pContext, 464 | _In_ DWORD nOrdinal, 465 | _In_opt_ LPCSTR pszFunc, 466 | _In_opt_ PVOID* ppvFunc); 467 | 468 | typedef VOID * PDETOUR_BINARY; 469 | typedef VOID * PDETOUR_LOADED_BINARY; 470 | 471 | //////////////////////////////////////////////////////////// Transaction APIs. 472 | // 473 | LONG WINAPI DetourTransactionBegin(VOID); 474 | LONG WINAPI DetourTransactionAbort(VOID); 475 | LONG WINAPI DetourTransactionCommit(VOID); 476 | LONG WINAPI DetourTransactionCommitEx(_Out_opt_ PVOID **pppFailedPointer); 477 | 478 | LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread); 479 | 480 | LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer, 481 | _In_ PVOID pDetour); 482 | 483 | LONG WINAPI DetourAttachEx(_Inout_ PVOID *ppPointer, 484 | _In_ PVOID pDetour, 485 | _Out_opt_ PDETOUR_TRAMPOLINE *ppRealTrampoline, 486 | _Out_opt_ PVOID *ppRealTarget, 487 | _Out_opt_ PVOID *ppRealDetour); 488 | 489 | LONG WINAPI DetourDetach(_Inout_ PVOID *ppPointer, 490 | _In_ PVOID pDetour); 491 | 492 | BOOL WINAPI DetourSetIgnoreTooSmall(_In_ BOOL fIgnore); 493 | BOOL WINAPI DetourSetRetainRegions(_In_ BOOL fRetain); 494 | PVOID WINAPI DetourSetSystemRegionLowerBound(_In_ PVOID pSystemRegionLowerBound); 495 | PVOID WINAPI DetourSetSystemRegionUpperBound(_In_ PVOID pSystemRegionUpperBound); 496 | 497 | ////////////////////////////////////////////////////////////// Code Functions. 498 | // 499 | PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule, 500 | _In_ LPCSTR pszFunction); 501 | PVOID WINAPI DetourCodeFromPointer(_In_ PVOID pPointer, 502 | _Out_opt_ PVOID *ppGlobals); 503 | PVOID WINAPI DetourCopyInstruction(_In_opt_ PVOID pDst, 504 | _Inout_opt_ PVOID *ppDstPool, 505 | _In_ PVOID pSrc, 506 | _Out_opt_ PVOID *ppTarget, 507 | _Out_opt_ LONG *plExtra); 508 | BOOL WINAPI DetourSetCodeModule(_In_ HMODULE hModule, 509 | _In_ BOOL fLimitReferencesToModule); 510 | 511 | ///////////////////////////////////////////////////// Loaded Binary Functions. 512 | // 513 | HMODULE WINAPI DetourGetContainingModule(_In_ PVOID pvAddr); 514 | HMODULE WINAPI DetourEnumerateModules(_In_opt_ HMODULE hModuleLast); 515 | PVOID WINAPI DetourGetEntryPoint(_In_opt_ HMODULE hModule); 516 | ULONG WINAPI DetourGetModuleSize(_In_opt_ HMODULE hModule); 517 | BOOL WINAPI DetourEnumerateExports(_In_ HMODULE hModule, 518 | _In_opt_ PVOID pContext, 519 | _In_ PF_DETOUR_ENUMERATE_EXPORT_CALLBACK pfExport); 520 | BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule, 521 | _In_opt_ PVOID pContext, 522 | _In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile, 523 | _In_opt_ PF_DETOUR_IMPORT_FUNC_CALLBACK pfImportFunc); 524 | 525 | BOOL WINAPI DetourEnumerateImportsEx(_In_opt_ HMODULE hModule, 526 | _In_opt_ PVOID pContext, 527 | _In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile, 528 | _In_opt_ PF_DETOUR_IMPORT_FUNC_CALLBACK_EX pfImportFuncEx); 529 | 530 | _Writable_bytes_(*pcbData) 531 | _Readable_bytes_(*pcbData) 532 | _Success_(return != NULL) 533 | PVOID WINAPI DetourFindPayload(_In_opt_ HMODULE hModule, 534 | _In_ REFGUID rguid, 535 | _Out_ DWORD *pcbData); 536 | 537 | _Writable_bytes_(*pcbData) 538 | _Readable_bytes_(*pcbData) 539 | _Success_(return != NULL) 540 | PVOID WINAPI DetourFindPayloadEx(_In_ REFGUID rguid, 541 | _Out_ DWORD * pcbData); 542 | 543 | DWORD WINAPI DetourGetSizeOfPayloads(_In_opt_ HMODULE hModule); 544 | 545 | ///////////////////////////////////////////////// Persistent Binary Functions. 546 | // 547 | 548 | PDETOUR_BINARY WINAPI DetourBinaryOpen(_In_ HANDLE hFile); 549 | 550 | _Writable_bytes_(*pcbData) 551 | _Readable_bytes_(*pcbData) 552 | _Success_(return != NULL) 553 | PVOID WINAPI DetourBinaryEnumeratePayloads(_In_ PDETOUR_BINARY pBinary, 554 | _Out_opt_ GUID *pGuid, 555 | _Out_ DWORD *pcbData, 556 | _Inout_ DWORD *pnIterator); 557 | 558 | _Writable_bytes_(*pcbData) 559 | _Readable_bytes_(*pcbData) 560 | _Success_(return != NULL) 561 | PVOID WINAPI DetourBinaryFindPayload(_In_ PDETOUR_BINARY pBinary, 562 | _In_ REFGUID rguid, 563 | _Out_ DWORD *pcbData); 564 | 565 | PVOID WINAPI DetourBinarySetPayload(_In_ PDETOUR_BINARY pBinary, 566 | _In_ REFGUID rguid, 567 | _In_reads_opt_(cbData) PVOID pData, 568 | _In_ DWORD cbData); 569 | BOOL WINAPI DetourBinaryDeletePayload(_In_ PDETOUR_BINARY pBinary, _In_ REFGUID rguid); 570 | BOOL WINAPI DetourBinaryPurgePayloads(_In_ PDETOUR_BINARY pBinary); 571 | BOOL WINAPI DetourBinaryResetImports(_In_ PDETOUR_BINARY pBinary); 572 | BOOL WINAPI DetourBinaryEditImports(_In_ PDETOUR_BINARY pBinary, 573 | _In_opt_ PVOID pContext, 574 | _In_opt_ PF_DETOUR_BINARY_BYWAY_CALLBACK pfByway, 575 | _In_opt_ PF_DETOUR_BINARY_FILE_CALLBACK pfFile, 576 | _In_opt_ PF_DETOUR_BINARY_SYMBOL_CALLBACK pfSymbol, 577 | _In_opt_ PF_DETOUR_BINARY_COMMIT_CALLBACK pfCommit); 578 | BOOL WINAPI DetourBinaryWrite(_In_ PDETOUR_BINARY pBinary, _In_ HANDLE hFile); 579 | BOOL WINAPI DetourBinaryClose(_In_ PDETOUR_BINARY pBinary); 580 | 581 | /////////////////////////////////////////////////// Create Process & Load Dll. 582 | // 583 | typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEA)( 584 | _In_opt_ LPCSTR lpApplicationName, 585 | _Inout_opt_ LPSTR lpCommandLine, 586 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 587 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 588 | _In_ BOOL bInheritHandles, 589 | _In_ DWORD dwCreationFlags, 590 | _In_opt_ LPVOID lpEnvironment, 591 | _In_opt_ LPCSTR lpCurrentDirectory, 592 | _In_ LPSTARTUPINFOA lpStartupInfo, 593 | _Out_ LPPROCESS_INFORMATION lpProcessInformation); 594 | 595 | typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEW)( 596 | _In_opt_ LPCWSTR lpApplicationName, 597 | _Inout_opt_ LPWSTR lpCommandLine, 598 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 599 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 600 | _In_ BOOL bInheritHandles, 601 | _In_ DWORD dwCreationFlags, 602 | _In_opt_ LPVOID lpEnvironment, 603 | _In_opt_ LPCWSTR lpCurrentDirectory, 604 | _In_ LPSTARTUPINFOW lpStartupInfo, 605 | _Out_ LPPROCESS_INFORMATION lpProcessInformation); 606 | 607 | BOOL WINAPI DetourCreateProcessWithDllA(_In_opt_ LPCSTR lpApplicationName, 608 | _Inout_opt_ LPSTR lpCommandLine, 609 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 610 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 611 | _In_ BOOL bInheritHandles, 612 | _In_ DWORD dwCreationFlags, 613 | _In_opt_ LPVOID lpEnvironment, 614 | _In_opt_ LPCSTR lpCurrentDirectory, 615 | _In_ LPSTARTUPINFOA lpStartupInfo, 616 | _Out_ LPPROCESS_INFORMATION lpProcessInformation, 617 | _In_ LPCSTR lpDllName, 618 | _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); 619 | 620 | BOOL WINAPI DetourCreateProcessWithDllW(_In_opt_ LPCWSTR lpApplicationName, 621 | _Inout_opt_ LPWSTR lpCommandLine, 622 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 623 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 624 | _In_ BOOL bInheritHandles, 625 | _In_ DWORD dwCreationFlags, 626 | _In_opt_ LPVOID lpEnvironment, 627 | _In_opt_ LPCWSTR lpCurrentDirectory, 628 | _In_ LPSTARTUPINFOW lpStartupInfo, 629 | _Out_ LPPROCESS_INFORMATION lpProcessInformation, 630 | _In_ LPCSTR lpDllName, 631 | _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); 632 | 633 | #ifdef UNICODE 634 | #define DetourCreateProcessWithDll DetourCreateProcessWithDllW 635 | #define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEW 636 | #else 637 | #define DetourCreateProcessWithDll DetourCreateProcessWithDllA 638 | #define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEA 639 | #endif // !UNICODE 640 | 641 | BOOL WINAPI DetourCreateProcessWithDllExA(_In_opt_ LPCSTR lpApplicationName, 642 | _Inout_opt_ LPSTR lpCommandLine, 643 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 644 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 645 | _In_ BOOL bInheritHandles, 646 | _In_ DWORD dwCreationFlags, 647 | _In_opt_ LPVOID lpEnvironment, 648 | _In_opt_ LPCSTR lpCurrentDirectory, 649 | _In_ LPSTARTUPINFOA lpStartupInfo, 650 | _Out_ LPPROCESS_INFORMATION lpProcessInformation, 651 | _In_ LPCSTR lpDllName, 652 | _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); 653 | 654 | BOOL WINAPI DetourCreateProcessWithDllExW(_In_opt_ LPCWSTR lpApplicationName, 655 | _Inout_opt_ LPWSTR lpCommandLine, 656 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 657 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 658 | _In_ BOOL bInheritHandles, 659 | _In_ DWORD dwCreationFlags, 660 | _In_opt_ LPVOID lpEnvironment, 661 | _In_opt_ LPCWSTR lpCurrentDirectory, 662 | _In_ LPSTARTUPINFOW lpStartupInfo, 663 | _Out_ LPPROCESS_INFORMATION lpProcessInformation, 664 | _In_ LPCSTR lpDllName, 665 | _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); 666 | 667 | #ifdef UNICODE 668 | #define DetourCreateProcessWithDllEx DetourCreateProcessWithDllExW 669 | #else 670 | #define DetourCreateProcessWithDllEx DetourCreateProcessWithDllExA 671 | #endif // !UNICODE 672 | 673 | BOOL WINAPI DetourCreateProcessWithDllsA(_In_opt_ LPCSTR lpApplicationName, 674 | _Inout_opt_ LPSTR lpCommandLine, 675 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 676 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 677 | _In_ BOOL bInheritHandles, 678 | _In_ DWORD dwCreationFlags, 679 | _In_opt_ LPVOID lpEnvironment, 680 | _In_opt_ LPCSTR lpCurrentDirectory, 681 | _In_ LPSTARTUPINFOA lpStartupInfo, 682 | _Out_ LPPROCESS_INFORMATION lpProcessInformation, 683 | _In_ DWORD nDlls, 684 | _In_reads_(nDlls) LPCSTR *rlpDlls, 685 | _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); 686 | 687 | BOOL WINAPI DetourCreateProcessWithDllsW(_In_opt_ LPCWSTR lpApplicationName, 688 | _Inout_opt_ LPWSTR lpCommandLine, 689 | _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, 690 | _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, 691 | _In_ BOOL bInheritHandles, 692 | _In_ DWORD dwCreationFlags, 693 | _In_opt_ LPVOID lpEnvironment, 694 | _In_opt_ LPCWSTR lpCurrentDirectory, 695 | _In_ LPSTARTUPINFOW lpStartupInfo, 696 | _Out_ LPPROCESS_INFORMATION lpProcessInformation, 697 | _In_ DWORD nDlls, 698 | _In_reads_(nDlls) LPCSTR *rlpDlls, 699 | _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); 700 | 701 | #ifdef UNICODE 702 | #define DetourCreateProcessWithDlls DetourCreateProcessWithDllsW 703 | #else 704 | #define DetourCreateProcessWithDlls DetourCreateProcessWithDllsA 705 | #endif // !UNICODE 706 | 707 | BOOL WINAPI DetourProcessViaHelperA(_In_ DWORD dwTargetPid, 708 | _In_ LPCSTR lpDllName, 709 | _In_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); 710 | 711 | BOOL WINAPI DetourProcessViaHelperW(_In_ DWORD dwTargetPid, 712 | _In_ LPCSTR lpDllName, 713 | _In_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); 714 | 715 | #ifdef UNICODE 716 | #define DetourProcessViaHelper DetourProcessViaHelperW 717 | #else 718 | #define DetourProcessViaHelper DetourProcessViaHelperA 719 | #endif // !UNICODE 720 | 721 | BOOL WINAPI DetourProcessViaHelperDllsA(_In_ DWORD dwTargetPid, 722 | _In_ DWORD nDlls, 723 | _In_reads_(nDlls) LPCSTR *rlpDlls, 724 | _In_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); 725 | 726 | BOOL WINAPI DetourProcessViaHelperDllsW(_In_ DWORD dwTargetPid, 727 | _In_ DWORD nDlls, 728 | _In_reads_(nDlls) LPCSTR *rlpDlls, 729 | _In_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); 730 | 731 | #ifdef UNICODE 732 | #define DetourProcessViaHelperDlls DetourProcessViaHelperDllsW 733 | #else 734 | #define DetourProcessViaHelperDlls DetourProcessViaHelperDllsA 735 | #endif // !UNICODE 736 | 737 | BOOL WINAPI DetourUpdateProcessWithDll(_In_ HANDLE hProcess, 738 | _In_reads_(nDlls) LPCSTR *rlpDlls, 739 | _In_ DWORD nDlls); 740 | 741 | BOOL WINAPI DetourUpdateProcessWithDllEx(_In_ HANDLE hProcess, 742 | _In_ HMODULE hImage, 743 | _In_ BOOL bIs32Bit, 744 | _In_reads_(nDlls) LPCSTR *rlpDlls, 745 | _In_ DWORD nDlls); 746 | 747 | BOOL WINAPI DetourCopyPayloadToProcess(_In_ HANDLE hProcess, 748 | _In_ REFGUID rguid, 749 | _In_reads_bytes_(cbData) PVOID pvData, 750 | _In_ DWORD cbData); 751 | BOOL WINAPI DetourRestoreAfterWith(VOID); 752 | BOOL WINAPI DetourRestoreAfterWithEx(_In_reads_bytes_(cbData) PVOID pvData, 753 | _In_ DWORD cbData); 754 | BOOL WINAPI DetourIsHelperProcess(VOID); 755 | VOID CALLBACK DetourFinishHelperProcess(_In_ HWND, 756 | _In_ HINSTANCE, 757 | _In_ LPSTR, 758 | _In_ INT); 759 | 760 | // 761 | ////////////////////////////////////////////////////////////////////////////// 762 | #ifdef __cplusplus 763 | } 764 | #endif // __cplusplus 765 | 766 | //////////////////////////////////////////////// Detours Internal Definitions. 767 | // 768 | #ifdef __cplusplus 769 | #ifdef DETOURS_INTERNAL 770 | 771 | #define NOTHROW 772 | // #define NOTHROW (nothrow) 773 | 774 | ////////////////////////////////////////////////////////////////////////////// 775 | // 776 | #if (_MSC_VER < 1299) 777 | #include 778 | typedef IMAGEHLP_MODULE IMAGEHLP_MODULE64; 779 | typedef PIMAGEHLP_MODULE PIMAGEHLP_MODULE64; 780 | typedef IMAGEHLP_SYMBOL SYMBOL_INFO; 781 | typedef PIMAGEHLP_SYMBOL PSYMBOL_INFO; 782 | 783 | static inline 784 | LONG InterlockedCompareExchange(_Inout_ LONG *ptr, _In_ LONG nval, _In_ LONG oval) 785 | { 786 | return (LONG)::InterlockedCompareExchange((PVOID*)ptr, (PVOID)nval, (PVOID)oval); 787 | } 788 | #else 789 | #pragma warning(push) 790 | #pragma warning(disable:4091) // empty typedef 791 | #include 792 | #pragma warning(pop) 793 | #endif 794 | 795 | #ifdef IMAGEAPI // defined by DBGHELP.H 796 | typedef LPAPI_VERSION (NTAPI *PF_ImagehlpApiVersionEx)(_In_ LPAPI_VERSION AppVersion); 797 | 798 | typedef BOOL (NTAPI *PF_SymInitialize)(_In_ HANDLE hProcess, 799 | _In_opt_ LPCSTR UserSearchPath, 800 | _In_ BOOL fInvadeProcess); 801 | typedef DWORD (NTAPI *PF_SymSetOptions)(_In_ DWORD SymOptions); 802 | typedef DWORD (NTAPI *PF_SymGetOptions)(VOID); 803 | typedef DWORD64 (NTAPI *PF_SymLoadModule64)(_In_ HANDLE hProcess, 804 | _In_opt_ HANDLE hFile, 805 | _In_ LPSTR ImageName, 806 | _In_opt_ LPSTR ModuleName, 807 | _In_ DWORD64 BaseOfDll, 808 | _In_opt_ DWORD SizeOfDll); 809 | typedef BOOL (NTAPI *PF_SymGetModuleInfo64)(_In_ HANDLE hProcess, 810 | _In_ DWORD64 qwAddr, 811 | _Out_ PIMAGEHLP_MODULE64 ModuleInfo); 812 | typedef BOOL (NTAPI *PF_SymFromName)(_In_ HANDLE hProcess, 813 | _In_ LPSTR Name, 814 | _Out_ PSYMBOL_INFO Symbol); 815 | 816 | typedef struct _DETOUR_SYM_INFO 817 | { 818 | HANDLE hProcess; 819 | HMODULE hDbgHelp; 820 | PF_ImagehlpApiVersionEx pfImagehlpApiVersionEx; 821 | PF_SymInitialize pfSymInitialize; 822 | PF_SymSetOptions pfSymSetOptions; 823 | PF_SymGetOptions pfSymGetOptions; 824 | PF_SymLoadModule64 pfSymLoadModule64; 825 | PF_SymGetModuleInfo64 pfSymGetModuleInfo64; 826 | PF_SymFromName pfSymFromName; 827 | } DETOUR_SYM_INFO, *PDETOUR_SYM_INFO; 828 | 829 | PDETOUR_SYM_INFO DetourLoadImageHlp(VOID); 830 | 831 | #endif // IMAGEAPI 832 | 833 | #if defined(_INC_STDIO) && !defined(_CRT_STDIO_ARBITRARY_WIDE_SPECIFIERS) 834 | #error detours.h must be included before stdio.h (or at least define _CRT_STDIO_ARBITRARY_WIDE_SPECIFIERS earlier) 835 | #endif 836 | #define _CRT_STDIO_ARBITRARY_WIDE_SPECIFIERS 1 837 | 838 | #ifndef DETOUR_TRACE 839 | #if DETOUR_DEBUG 840 | #define DETOUR_TRACE(x) printf x 841 | #define DETOUR_BREAK() __debugbreak() 842 | #include 843 | #include 844 | #else 845 | #define DETOUR_TRACE(x) 846 | #define DETOUR_BREAK() 847 | #endif 848 | #endif 849 | 850 | #if 1 || defined(DETOURS_IA64) 851 | 852 | // 853 | // IA64 instructions are 41 bits, 3 per bundle, plus 5 bit bundle template => 128 bits per bundle. 854 | // 855 | 856 | #define DETOUR_IA64_INSTRUCTIONS_PER_BUNDLE (3) 857 | 858 | #define DETOUR_IA64_TEMPLATE_OFFSET (0) 859 | #define DETOUR_IA64_TEMPLATE_SIZE (5) 860 | 861 | #define DETOUR_IA64_INSTRUCTION_SIZE (41) 862 | #define DETOUR_IA64_INSTRUCTION0_OFFSET (DETOUR_IA64_TEMPLATE_SIZE) 863 | #define DETOUR_IA64_INSTRUCTION1_OFFSET (DETOUR_IA64_TEMPLATE_SIZE + DETOUR_IA64_INSTRUCTION_SIZE) 864 | #define DETOUR_IA64_INSTRUCTION2_OFFSET (DETOUR_IA64_TEMPLATE_SIZE + DETOUR_IA64_INSTRUCTION_SIZE + DETOUR_IA64_INSTRUCTION_SIZE) 865 | 866 | C_ASSERT(DETOUR_IA64_TEMPLATE_SIZE + DETOUR_IA64_INSTRUCTIONS_PER_BUNDLE * DETOUR_IA64_INSTRUCTION_SIZE == 128); 867 | 868 | __declspec(align(16)) struct DETOUR_IA64_BUNDLE 869 | { 870 | public: 871 | union 872 | { 873 | BYTE data[16]; 874 | UINT64 wide[2]; 875 | }; 876 | 877 | enum { 878 | A_UNIT = 1u, 879 | I_UNIT = 2u, 880 | M_UNIT = 3u, 881 | B_UNIT = 4u, 882 | F_UNIT = 5u, 883 | L_UNIT = 6u, 884 | X_UNIT = 7u, 885 | }; 886 | struct DETOUR_IA64_METADATA 887 | { 888 | ULONG nTemplate : 8; // Instruction template. 889 | ULONG nUnit0 : 4; // Unit for slot 0 890 | ULONG nUnit1 : 4; // Unit for slot 1 891 | ULONG nUnit2 : 4; // Unit for slot 2 892 | }; 893 | 894 | protected: 895 | static const DETOUR_IA64_METADATA s_rceCopyTable[33]; 896 | 897 | UINT RelocateBundle(_Inout_ DETOUR_IA64_BUNDLE* pDst, _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra) const; 898 | 899 | bool RelocateInstruction(_Inout_ DETOUR_IA64_BUNDLE* pDst, 900 | _In_ BYTE slot, 901 | _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra) const; 902 | 903 | // 120 112 104 96 88 80 72 64 56 48 40 32 24 16 8 0 904 | // f. e. d. c. b. a. 9. 8. 7. 6. 5. 4. 3. 2. 1. 0. 905 | 906 | // 00 907 | // f.e. d.c. b.a. 9.8. 7.6. 5.4. 3.2. 1.0. 908 | // 0000 0000 0000 0000 0000 0000 0000 001f : Template [4..0] 909 | // 0000 0000 0000 0000 0000 03ff ffff ffe0 : Zero [ 41.. 5] 910 | // 0000 0000 0000 0000 0000 3c00 0000 0000 : Zero [ 45.. 42] 911 | // 0000 0000 0007 ffff ffff c000 0000 0000 : One [ 82.. 46] 912 | // 0000 0000 0078 0000 0000 0000 0000 0000 : One [ 86.. 83] 913 | // 0fff ffff ff80 0000 0000 0000 0000 0000 : Two [123.. 87] 914 | // f000 0000 0000 0000 0000 0000 0000 0000 : Two [127..124] 915 | BYTE GetTemplate() const; 916 | // Get 4 bit opcodes. 917 | BYTE GetInst0() const; 918 | BYTE GetInst1() const; 919 | BYTE GetInst2() const; 920 | BYTE GetUnit(BYTE slot) const; 921 | BYTE GetUnit0() const; 922 | BYTE GetUnit1() const; 923 | BYTE GetUnit2() const; 924 | // Get 37 bit data. 925 | UINT64 GetData0() const; 926 | UINT64 GetData1() const; 927 | UINT64 GetData2() const; 928 | 929 | // Get/set the full 41 bit instructions. 930 | UINT64 GetInstruction(BYTE slot) const; 931 | UINT64 GetInstruction0() const; 932 | UINT64 GetInstruction1() const; 933 | UINT64 GetInstruction2() const; 934 | void SetInstruction(BYTE slot, UINT64 instruction); 935 | void SetInstruction0(UINT64 instruction); 936 | void SetInstruction1(UINT64 instruction); 937 | void SetInstruction2(UINT64 instruction); 938 | 939 | // Get/set bitfields. 940 | static UINT64 GetBits(UINT64 Value, UINT64 Offset, UINT64 Count); 941 | static UINT64 SetBits(UINT64 Value, UINT64 Offset, UINT64 Count, UINT64 Field); 942 | 943 | // Get specific read-only fields. 944 | static UINT64 GetOpcode(UINT64 instruction); // 4bit opcode 945 | static UINT64 GetX(UINT64 instruction); // 1bit opcode extension 946 | static UINT64 GetX3(UINT64 instruction); // 3bit opcode extension 947 | static UINT64 GetX6(UINT64 instruction); // 6bit opcode extension 948 | 949 | // Get/set specific fields. 950 | static UINT64 GetImm7a(UINT64 instruction); 951 | static UINT64 SetImm7a(UINT64 instruction, UINT64 imm7a); 952 | static UINT64 GetImm13c(UINT64 instruction); 953 | static UINT64 SetImm13c(UINT64 instruction, UINT64 imm13c); 954 | static UINT64 GetSignBit(UINT64 instruction); 955 | static UINT64 SetSignBit(UINT64 instruction, UINT64 signBit); 956 | static UINT64 GetImm20a(UINT64 instruction); 957 | static UINT64 SetImm20a(UINT64 instruction, UINT64 imm20a); 958 | static UINT64 GetImm20b(UINT64 instruction); 959 | static UINT64 SetImm20b(UINT64 instruction, UINT64 imm20b); 960 | 961 | static UINT64 SignExtend(UINT64 Value, UINT64 Offset); 962 | 963 | BOOL IsMovlGp() const; 964 | 965 | VOID SetInst(BYTE Slot, BYTE nInst); 966 | VOID SetInst0(BYTE nInst); 967 | VOID SetInst1(BYTE nInst); 968 | VOID SetInst2(BYTE nInst); 969 | VOID SetData(BYTE Slot, UINT64 nData); 970 | VOID SetData0(UINT64 nData); 971 | VOID SetData1(UINT64 nData); 972 | VOID SetData2(UINT64 nData); 973 | BOOL SetNop(BYTE Slot); 974 | BOOL SetNop0(); 975 | BOOL SetNop1(); 976 | BOOL SetNop2(); 977 | 978 | public: 979 | BOOL IsBrl() const; 980 | VOID SetBrl(); 981 | VOID SetBrl(UINT64 target); 982 | UINT64 GetBrlTarget() const; 983 | VOID SetBrlTarget(UINT64 target); 984 | VOID SetBrlImm(UINT64 imm); 985 | UINT64 GetBrlImm() const; 986 | 987 | UINT64 GetMovlGp() const; 988 | VOID SetMovlGp(UINT64 gp); 989 | 990 | VOID SetStop(); 991 | 992 | UINT Copy(_Out_ DETOUR_IA64_BUNDLE *pDst, _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra = NULL) const; 993 | }; 994 | #endif // DETOURS_IA64 995 | 996 | #ifdef DETOURS_ARM 997 | 998 | #define DETOURS_PFUNC_TO_PBYTE(p) ((PBYTE)(((ULONG_PTR)(p)) & ~(ULONG_PTR)1)) 999 | #define DETOURS_PBYTE_TO_PFUNC(p) ((PBYTE)(((ULONG_PTR)(p)) | (ULONG_PTR)1)) 1000 | 1001 | #endif // DETOURS_ARM 1002 | 1003 | ////////////////////////////////////////////////////////////////////////////// 1004 | 1005 | #ifdef __cplusplus 1006 | extern "C" { 1007 | #endif // __cplusplus 1008 | 1009 | #define DETOUR_OFFLINE_LIBRARY(x) \ 1010 | PVOID WINAPI DetourCopyInstruction##x(_In_opt_ PVOID pDst, \ 1011 | _Inout_opt_ PVOID *ppDstPool, \ 1012 | _In_ PVOID pSrc, \ 1013 | _Out_opt_ PVOID *ppTarget, \ 1014 | _Out_opt_ LONG *plExtra); \ 1015 | \ 1016 | BOOL WINAPI DetourSetCodeModule##x(_In_ HMODULE hModule, \ 1017 | _In_ BOOL fLimitReferencesToModule); \ 1018 | 1019 | DETOUR_OFFLINE_LIBRARY(X86) 1020 | DETOUR_OFFLINE_LIBRARY(X64) 1021 | DETOUR_OFFLINE_LIBRARY(ARM) 1022 | DETOUR_OFFLINE_LIBRARY(ARM64) 1023 | DETOUR_OFFLINE_LIBRARY(IA64) 1024 | 1025 | #undef DETOUR_OFFLINE_LIBRARY 1026 | 1027 | ////////////////////////////////////////////////////////////////////////////// 1028 | // 1029 | // Helpers for manipulating page protection. 1030 | // 1031 | 1032 | _Success_(return != FALSE) 1033 | BOOL WINAPI DetourVirtualProtectSameExecuteEx(_In_ HANDLE hProcess, 1034 | _In_ PVOID pAddress, 1035 | _In_ SIZE_T nSize, 1036 | _In_ DWORD dwNewProtect, 1037 | _Out_ PDWORD pdwOldProtect); 1038 | 1039 | _Success_(return != FALSE) 1040 | BOOL WINAPI DetourVirtualProtectSameExecute(_In_ PVOID pAddress, 1041 | _In_ SIZE_T nSize, 1042 | _In_ DWORD dwNewProtect, 1043 | _Out_ PDWORD pdwOldProtect); 1044 | #ifdef __cplusplus 1045 | } 1046 | #endif // __cplusplus 1047 | 1048 | ////////////////////////////////////////////////////////////////////////////// 1049 | 1050 | #define MM_ALLOCATION_GRANULARITY 0x10000 1051 | 1052 | ////////////////////////////////////////////////////////////////////////////// 1053 | 1054 | #endif // DETOURS_INTERNAL 1055 | #endif // __cplusplus 1056 | 1057 | #endif // _DETOURS_H_ 1058 | // 1059 | //////////////////////////////////////////////////////////////// End of File. 1060 | -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/include/detver.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Common version parameters. 4 | // 5 | // Microsoft Research Detours Package, Version 4.0.1 6 | // 7 | // Copyright (c) Microsoft Corporation. All rights reserved. 8 | // 9 | 10 | #define _USING_V110_SDK71_ 1 11 | #include "winver.h" 12 | #if 0 13 | #include 14 | #include 15 | #else 16 | #ifndef DETOURS_STRINGIFY 17 | #define DETOURS_STRINGIFY(x) DETOURS_STRINGIFY_(x) 18 | #define DETOURS_STRINGIFY_(x) #x 19 | #endif 20 | 21 | #define VER_FILEFLAGSMASK 0x3fL 22 | #define VER_FILEFLAGS 0x0L 23 | #define VER_FILEOS 0x00040004L 24 | #define VER_FILETYPE 0x00000002L 25 | #define VER_FILESUBTYPE 0x00000000L 26 | #endif 27 | #define VER_DETOURS_BITS DETOUR_STRINGIFY(DETOURS_BITS) 28 | -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/include/syelog.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Detours Test Program (syelog.h of syelog.lib) 4 | // 5 | // Microsoft Research Detours Package 6 | // 7 | // Copyright (c) Microsoft Corporation. All rights reserved. 8 | // 9 | #pragma once 10 | #ifndef _SYELOGD_H_ 11 | #define _SYELOGD_H_ 12 | #include 13 | 14 | #pragma pack(push, 1) 15 | #pragma warning(push) 16 | #pragma warning(disable: 4200) 17 | 18 | ////////////////////////////////////////////////////////////////////////////// 19 | // 20 | // 21 | #define SYELOG_PIPE_NAMEA "\\\\.\\pipe\\syelog" 22 | #define SYELOG_PIPE_NAMEW L"\\\\.\\pipe\\syelog" 23 | #ifdef UNICODE 24 | #define SYELOG_PIPE_NAME SYELOG_PIPE_NAMEW 25 | #else 26 | #define SYELOG_PIPE_NAME SYELOG_PIPE_NAMEA 27 | #endif 28 | 29 | ////////////////////////////////////////////////////////////////////////////// 30 | // 31 | #define SYELOG_MAXIMUM_MESSAGE 4086 // 4096 - sizeof(header stuff) 32 | 33 | typedef struct _SYELOG_MESSAGE 34 | { 35 | USHORT nBytes; 36 | BYTE nFacility; 37 | BYTE nSeverity; 38 | DWORD nProcessId; 39 | FILETIME ftOccurance; 40 | BOOL fTerminate; 41 | CHAR szMessage[SYELOG_MAXIMUM_MESSAGE]; 42 | } SYELOG_MESSAGE, *PSYELOG_MESSAGE; 43 | 44 | 45 | // Facility Codes. 46 | // 47 | #define SYELOG_FACILITY_KERNEL 0x10 // OS Kernel 48 | #define SYELOG_FACILITY_SECURITY 0x20 // OS Security 49 | #define SYELOG_FACILITY_LOGGING 0x30 // OS Logging-internal 50 | #define SYELOG_FACILITY_SERVICE 0x40 // User-mode system daemon 51 | #define SYELOG_FACILITY_APPLICATION 0x50 // User-mode application 52 | #define SYELOG_FACILITY_USER 0x60 // User self-generated. 53 | #define SYELOG_FACILITY_LOCAL0 0x70 // Locally defined. 54 | #define SYELOG_FACILITY_LOCAL1 0x71 // Locally defined. 55 | #define SYELOG_FACILITY_LOCAL2 0x72 // Locally defined. 56 | #define SYELOG_FACILITY_LOCAL3 0x73 // Locally defined. 57 | #define SYELOG_FACILITY_LOCAL4 0x74 // Locally defined. 58 | #define SYELOG_FACILITY_LOCAL5 0x75 // Locally defined. 59 | #define SYELOG_FACILITY_LOCAL6 0x76 // Locally defined. 60 | #define SYELOG_FACILITY_LOCAL7 0x77 // Locally defined. 61 | #define SYELOG_FACILITY_LOCAL8 0x78 // Locally defined. 62 | #define SYELOG_FACILITY_LOCAL9 0x79 // Locally defined. 63 | 64 | // Severity Codes. 65 | // 66 | #define SYELOG_SEVERITY_FATAL 0x00 // System is dead. 67 | #define SYELOG_SEVERITY_ALERT 0x10 // Take action immediately. 68 | #define SYELOG_SEVERITY_CRITICAL 0x20 // Critical condition. 69 | #define SYELOG_SEVERITY_ERROR 0x30 // Error 70 | #define SYELOG_SEVERITY_WARNING 0x40 // Warning 71 | #define SYELOG_SEVERITY_NOTICE 0x50 // Significant condition. 72 | #define SYELOG_SEVERITY_INFORMATION 0x60 // Informational 73 | #define SYELOG_SEVERITY_AUDIT_FAIL 0x66 // Audit Failed 74 | #define SYELOG_SEVERITY_AUDIT_PASS 0x67 // Audit Succeeeded 75 | #define SYELOG_SEVERITY_DEBUG 0x70 // Debugging 76 | 77 | // Logging Functions. 78 | // 79 | VOID SyelogOpen(PCSTR pszIdentifier, BYTE nFacility); 80 | VOID Syelog(BYTE nSeverity, PCSTR pszMsgf, ...); 81 | VOID SyelogV(BYTE nSeverity, PCSTR pszMsgf, va_list args); 82 | VOID SyelogClose(BOOL fTerminate); 83 | 84 | #pragma warning(pop) 85 | #pragma pack(pop) 86 | 87 | #endif // _SYELOGD_H_ 88 | // 89 | ///////////////////////////////////////////////////////////////// End of File. 90 | -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/lib.X86/detours.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HIllya51/MuramasaHorizontal/f4adbb1ec7ea5bdc0796ace514174d0f0a297207/muramasa/patch/Detours-4.0.1/lib.X86/detours.lib -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/lib.X86/detours.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HIllya51/MuramasaHorizontal/f4adbb1ec7ea5bdc0796ace514174d0f0a297207/muramasa/patch/Detours-4.0.1/lib.X86/detours.pdb -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/lib.X86/syelog.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HIllya51/MuramasaHorizontal/f4adbb1ec7ea5bdc0796ace514174d0f0a297207/muramasa/patch/Detours-4.0.1/lib.X86/syelog.lib -------------------------------------------------------------------------------- /muramasa/patch/Detours-4.0.1/system.mak: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | ## 3 | ## Establish build target type for Detours. 4 | ## 5 | ## Microsoft Research Detours Package 6 | ## 7 | ## Copyright (c) Microsoft Corporation. All rights reserved. 8 | ## 9 | 10 | ############################################## Determine Processor Build Type. 11 | ## 12 | !IF "$(DETOURS_TARGET_PROCESSOR)" == "" && "$(PROCESSOR_ARCHITEW6432)" != "" 13 | DETOURS_TARGET_PROCESSOR = X86 14 | !ENDIF 15 | 16 | !IF "$(DETOURS_TARGET_PROCESSOR)" == "" 17 | DETOURS_TARGET_PROCESSOR = $(PROCESSOR_ARCHITECTURE) 18 | !ENDIF 19 | 20 | # uppercase DETOURS_TARGET_PROCESSOR 21 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:a=A) 22 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:b=B) 23 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:c=C) 24 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:d=D) 25 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:e=E) 26 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:f=F) 27 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:g=G) 28 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:h=H) 29 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:i=I) 30 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:j=J) 31 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:k=K) 32 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:l=L) 33 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:m=M) 34 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:n=N) 35 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:o=O) 36 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:p=P) 37 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:q=Q) 38 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:r=R) 39 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:s=S) 40 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:t=T) 41 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:u=U) 42 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:v=V) 43 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:w=W) 44 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:x=X) 45 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:y=Y) 46 | DETOURS_TARGET_PROCESSOR=$(DETOURS_TARGET_PROCESSOR:z=Z) 47 | 48 | !IF "$(DETOURS_TARGET_PROCESSOR)" == "AMD64" 49 | DETOURS_TARGET_PROCESSOR = X64 50 | !ENDIF 51 | 52 | 53 | !if "$(DETOURS_TARGET_PROCESSOR:64=)" == "$(DETOURS_TARGET_PROCESSOR)" 54 | DETOURS_32BIT=1 55 | DETOURS_BITS=32 56 | !else 57 | DETOURS_64BIT=1 58 | DETOURS_BITS=64 59 | !endif 60 | 61 | ########################################## Configure build based on Processor. 62 | ## 63 | ## DETOURS_OPTION_PROCESSOR: Set this macro if the processor *will* run code 64 | ## from another ISA (i.e. x86 on x64). 65 | ## 66 | ## DETOURS_OPTION_BITS: Set this macro if the processor *may* have an 67 | ## an alternative word size. 68 | ## 69 | !IF "$(DETOURS_TARGET_PROCESSOR)" == "X64" 70 | #!MESSAGE Building for 64-bit X64. 71 | DETOURS_SOURCE_BROWSING = 0 72 | DETOURS_OPTION_PROCESSOR=X86 73 | DETOURS_OPTION_BITS=32 74 | !ELSEIF "$(DETOURS_TARGET_PROCESSOR)" == "IA64" 75 | #!MESSAGE Building for 64-bit IA64. 76 | DETOURS_OPTION_PROCESSOR=X86 77 | DETOURS_OPTION_BITS=32 78 | !ELSEIF "$(DETOURS_TARGET_PROCESSOR)" == "X86" 79 | #!MESSAGE Building for 32-bit X86. 80 | DETOURS_OPTION_BITS=64 81 | # Don't set DETOURS_OPTION_PROCESSOR for x64 because we don't *know* that 82 | # we'll run on a 64-bit machine. 83 | !ELSEIF "$(DETOURS_TARGET_PROCESSOR)" == "ARM" 84 | #!MESSAGE Building for 32-bit ARM. 85 | !ELSEIF "$(DETOURS_TARGET_PROCESSOR)" == "ARM64" 86 | #!MESSAGE Building for 64-bit ARM. 87 | !ELSE 88 | !MESSAGE Note: To select the target processor architecture set either 89 | !MESSAGE PROCESSOR_ARCHITECTURE or DETOURS_TARGET_PROCESSOR. 90 | !MESSAGE 91 | !ERROR Unknown target processor: $(DETOURS_TARGET_ARCHITECTURE) 92 | !ENDIF 93 | 94 | ############################################################################## 95 | ## 96 | INCD = $(ROOT)\include 97 | LIBD = $(ROOT)\lib.$(DETOURS_TARGET_PROCESSOR)$(DETOURS_CONFIG) 98 | BIND = $(ROOT)\bin.$(DETOURS_TARGET_PROCESSOR)$(DETOURS_CONFIG) 99 | OBJD = obj.$(DETOURS_TARGET_PROCESSOR)$(DETOURS_CONFIG) 100 | !IF "$(DETOURS_OPTION_PROCESSOR)" != "" 101 | OPTD = $(ROOT)\bin.$(DETOURS_OPTION_PROCESSOR)$(DETOURS_CONFIG) 102 | !ENDIF 103 | 104 | INCDS = $(ROOT)\include 105 | 106 | LIBDS = \ 107 | $(ROOT)\lib.x86$(DETOURS_CONFIG) \ 108 | $(ROOT)\lib.x64$(DETOURS_CONFIG) \ 109 | $(ROOT)\lib.ia64$(DETOURS_CONFIG) \ 110 | $(ROOT)\lib.arm$(DETOURS_CONFIG) \ 111 | $(ROOT)\lib.arm64$(DETOURS_CONFIG) \ 112 | 113 | BINDS = \ 114 | $(ROOT)\bin.x86$(DETOURS_CONFIG) \ 115 | $(ROOT)\bin.x64$(DETOURS_CONFIG) \ 116 | $(ROOT)\bin.ia64$(DETOURS_CONFIG) \ 117 | $(ROOT)\bin.arm$(DETOURS_CONFIG) \ 118 | $(ROOT)\bin.arm64$(DETOURS_CONFIG) \ 119 | 120 | OBJDS = \ 121 | obj.x86$(DETOURS_CONFIG) \ 122 | obj.x64$(DETOURS_CONFIG) \ 123 | obj.ia64$(DETOURS_CONFIG) \ 124 | obj.arm$(DETOURS_CONFIG) \ 125 | obj.arm64$(DETOURS_CONFIG) \ 126 | 127 | ############################################################################## 128 | -------------------------------------------------------------------------------- /muramasa/patch/loader.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow) 6 | { 7 | 8 | auto dllpath = std::filesystem::current_path() /"Horizontal.dll"; 9 | auto s=new char [dllpath.string().size()+1]; 10 | strcpy(s,dllpath.string().c_str()); 11 | 12 | STARTUPINFOW si; 13 | PROCESS_INFORMATION pi; 14 | ZeroMemory(&si, sizeof(STARTUPINFO)); 15 | ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); 16 | si.cb = sizeof(STARTUPINFO); 17 | 18 | wchar_t command[]=L".\\Muramasa_chs.exe"; 19 | DetourCreateProcessWithDllExW(NULL, command, NULL, 20 | NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, 21 | &si, &pi, s, NULL); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /muramasa/patch/patch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | __declspec(dllexport) void dumy() {} 13 | 14 | HMODULE g_hm; 15 | std::string LoadResImage(LPCWSTR pszResID) 16 | { 17 | HMODULE hModule=g_hm; 18 | HRSRC hRsrc = ::FindResource (hModule, pszResID,L"DATA"); // type 19 | if (!hRsrc) 20 | return 0; 21 | // load resource into memory 22 | DWORD len = SizeofResource(hModule, hRsrc); 23 | BYTE* lpRsrc = (BYTE*)LoadResource(hModule, hRsrc); 24 | if (!lpRsrc) 25 | return 0; 26 | // Allocate global memory on which to create stream 27 | HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len); 28 | BYTE* pmem = (BYTE*)GlobalLock(m_hMem); 29 | memcpy(pmem,lpRsrc,len); 30 | 31 | GlobalUnlock(m_hMem); 32 | 33 | FreeResource(lpRsrc); 34 | return std::string((char*)pmem,len); 35 | } 36 | inline LPWSTR mbwc(LPCSTR lstr, UINT CodePage = CP_ACP) 37 | { 38 | int lsize = strlen(lstr); 39 | int wsize = (lsize + 1) << 1; 40 | auto wstr = new wchar_t[wsize]; 41 | auto n = MultiByteToWideChar(CodePage, 0, lstr, lsize, wstr, wsize); 42 | wstr[n] = L'\0'; 43 | return wstr; 44 | } 45 | HWND WINAPI HookCreateWindowExA( 46 | DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, 47 | int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) 48 | { 49 | auto Title=new char[strlen(lpWindowName)+100]; 50 | strcpy(Title,lpWindowName); 51 | strcat(Title," 横向文字补丁 by HIllya51"); 52 | return CreateWindowExW(dwExStyle,mbwc(lpClassName),mbwc(Title),dwStyle,X,Y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam);; 53 | } 54 | 55 | auto OriginalCreateWindowExA=CreateWindowExA; 56 | auto OriginalCreateFileA=CreateFileA; 57 | wchar_t temp_file[MAX_PATH]; 58 | HANDLE WINAPI HookCreateFileA( 59 | _In_ LPCSTR lpFileName, 60 | _In_ DWORD dwDesiredAccess, 61 | _In_ DWORD dwShareMode, 62 | _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, 63 | _In_ DWORD dwCreationDisposition, 64 | _In_ DWORD dwFlagsAndAttributes, 65 | _In_opt_ HANDLE hTemplateFile 66 | ) 67 | { 68 | if(strcmp(lpFileName,"nss.npa")==0||strstr(lpFileName,"\\nss.npa")){ 69 | return CreateFileW(temp_file,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile); 70 | } 71 | return OriginalCreateFileA(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile); 72 | } 73 | void AttachFunctions() 74 | { 75 | DetourAttach(&(PVOID&)OriginalCreateFileA, HookCreateFileA); 76 | DetourAttach(&(PVOID&)OriginalCreateWindowExA, HookCreateWindowExA); 77 | } 78 | 79 | BOOL APIENTRY DllMain( HMODULE hModule, 80 | DWORD ul_reason_for_call, 81 | LPVOID lpReserved 82 | ) 83 | { 84 | if (DetourIsHelperProcess()) { 85 | return TRUE; 86 | } 87 | switch (ul_reason_for_call) 88 | { 89 | case DLL_PROCESS_ATTACH: { 90 | g_hm=hModule; 91 | auto nssdata=LoadResImage(L"NSSNPA"); 92 | wchar_t temp_path[MAX_PATH]; 93 | GetTempPathW(MAX_PATH, temp_path); 94 | 95 | 96 | GetTempFileNameW(temp_path, L"nss.npa", 0, temp_file); 97 | 98 | FILE *f=_wfopen(temp_file,L"wb"); 99 | fwrite(nssdata.c_str(),1,nssdata.size(),f); 100 | fclose(f); 101 | 102 | DetourRestoreAfterWith(); 103 | DetourTransactionBegin(); 104 | DetourUpdateThread(GetCurrentThread()); 105 | 106 | AttachFunctions(); 107 | 108 | DetourTransactionCommit(); 109 | break; 110 | } 111 | case DLL_THREAD_ATTACH: 112 | break; 113 | case DLL_THREAD_DETACH: 114 | break; 115 | case DLL_PROCESS_DETACH: 116 | DeleteFile(temp_file); 117 | break; 118 | 119 | } 120 | return TRUE; 121 | } -------------------------------------------------------------------------------- /muramasa/patch/patch.rc: -------------------------------------------------------------------------------- 1 | NSSNPA DATA "../nss.npa" -------------------------------------------------------------------------------- /muramasa/solve.py: -------------------------------------------------------------------------------- 1 | import os,re 2 | funs={ 3 | 'TypeBeginCI':'TypeBeginHI', 4 | 'TypeBeginCO':'TypeBeginHO', 5 | 'TypeBeginCIO':'TypeBeginHIO', 6 | 'TypeBeginCIFO':'TypeBeginHIFO', 7 | 'TypeBeginRI':'TypeBeginHI', 8 | 'TypeBeginRIFO':'TypeBeginHIFO', 9 | 'TypeBeginRO':'TypeBeginHO', 10 | 'TypeBeginRIO':'TypeBeginHIO', 11 | 'TypeBeginLI':'TypeBeginHI', 12 | 'TypeBeginLIFO':'TypeBeginHIFO', 13 | 'TypeBeginLO':'TypeBeginHO', 14 | 'TypeBeginLIO':'TypeBeginHIO', 15 | 'TypeBeginTimeCI':'TypeBeginTimeHI', 16 | 'TypeBeginTimeCO':'TypeBeginTimeHO', 17 | 'TypeBeginTimeCIO':'TypeBeginTimeHIO', 18 | 'TypeBeginTimeCIFO':'TypeBeginTimeHIFO', 19 | 'TypeBeginTimeRI':'TypeBeginTimeHI', 20 | 'TypeBeginTimeRO':'TypeBeginTimeHO', 21 | 'TypeBeginTimeRIFO':'TypeBeginTimeHIFO', 22 | 'TypeBeginTimeRIO':'TypeBeginTimeHIO', 23 | 'TypeBeginTimeLI':'TypeBeginTimeHI', 24 | 'TypeBeginTimeLIFO':'TypeBeginTimeHIFO', 25 | 'TypeBeginTimeLO':'TypeBeginTimeHO', 26 | 'TypeBeginTimeLIO':'TypeBeginTimeHIO', 27 | 'SetTextEXC':'SetTextEXH', 28 | 'SetTextEXR':'SetTextEXH', 29 | 'SetTextEXL':'SetTextEXH', 30 | 'SetTextEXCH':'SetTextEXH', 31 | 'SetTextA':'SetTextAH', 32 | 'SetTextEXhyperR':'SetTextEXH', 33 | 'TypeBeginNOHIO':'TypeBeginHIO', 34 | 'TypeBeginEXIO':'TypeBeginHIO', 35 | 'SetFwC':'SetFwH', 36 | 'SetFwL':'SetFwH', 37 | 'SetFwR':'SetFwH', 38 | 'SetNwC':'SetNwH', 39 | 'SetNwL':'SetNwH', 40 | 'SetNwR':'SetNwH', 41 | 'FwC':'FwH', 42 | 'FwR':'FwH', 43 | 'FwL':'FwH', 44 | 'NwC':'NwH', 45 | 'NwR':'NwH', 46 | 'NwL':'NwH', 47 | # 'OnFwC':'FwH' 48 | # 'SetTextEXCColor' 49 | } 50 | fun2={ 51 | 'SetTextEXCColor("RED");':'SetTextEXH();', 52 | 'SetTextEXCColor("#990000");':'SetTextEXH();', 53 | } 54 | for f in os.listdir('nss.origin'): 55 | #if f.startswith('mc01_012')==False:continue 56 | with open('nss.origin/'+f,'r',encoding='gbk',errors='ignore')as ff: 57 | t=ff.read() 58 | t=t.replace('nss/0_boot_開始スクリプト.nss','nss/0_boot_kaisscript.nss') 59 | 60 | if f.startswith('m')==False: 61 | with open('nss/'+f,'w',encoding='gbk') as ff: 62 | ff.write(t) 63 | continue 64 | 65 | for k in funs: 66 | t=t.replace(f'{k}(',f'{funs[k]}(') 67 | 68 | tlines=t.split('\n') 69 | started=False 70 | lengthlimit=len(' 因为吝于切换所需要的时间,直接以五为单位将热量平均')-1 71 | huakuohao=False 72 | for i in range(len(tlines)): 73 | if tlines[i].startswith('
'):
 74 |             started=True
 75 |         elif tlines[i].startswith('
'): 76 | started=False 77 | elif started: 78 | if huakuohao: 79 | if '}' in tlines[i]: 80 | huakuohao=False 81 | continue 82 | if tlines[i].startswith('//') or tlines[i].startswith('<') or tlines[i].startswith('['): 83 | newblock=True 84 | elif tlines[i].startswith('{'): 85 | huakuohao=True 86 | if '}' in tlines[i]: 87 | huakuohao=False 88 | elif tlines[i].strip()=='': 89 | continue 90 | else: 91 | sub=re.sub('<(.*?)>','',tlines[i]) 92 | if len(tlines[i+1])==0:continue 93 | if tlines[i+1][0] in ' /<[{《':continue 94 | if len(sub)>lengthlimit: 95 | print(f,tlines[i],'——',tlines[i+1]) 96 | tolong=tlines[i][lengthlimit:] 97 | tlines[i]=tlines[i][:lengthlimit] 98 | tlines[i+1]=tolong+tlines[i+1] 99 | t='\n'.join(tlines) 100 | 101 | 102 | with open('nss/'+f,'w',encoding='gbk') as ff: 103 | ff.write(t) 104 | os.rename('nss/0_boot_饑ハシ・ケ・ッ・・ラ・ネ.nss','nss/0_boot_kaisscript.nss') --------------------------------------------------------------------------------