2021-07-08 09:07:29 +03:00
from __future__ import with_statement
import os
from common_names import *
from collections import defaultdict as defdict
#format - engine name : list of source files
va_macro_src = [ ( " engines \\ sci " , " resource_patcher.cpp " ) , ( " engines \\ ags " , " managed_object_pool.h " ) ,
( " graphics \\ macgui " , " mactext.cpp " ) ]
2021-07-12 11:12:39 +03:00
#Format: "file to edit" = "parent folder", src, dst
oneline_patch_src = [
( " ptr.h " , [ " common " , r ' #include <cstddef> ' , r ' //#include <cstddef> ' ] ) ,
2021-08-01 17:38:41 +03:00
( " object.cpp " , [ " gui " , ''' error( " Unable to load widget position for \' %s \' . Please check your theme files " , _name.c_str()); ''' , ''' warning( " Unable to load widget position for \' %s \' . Please check your theme files " , _name.c_str()); ''' ] ) ,
2021-07-12 11:12:39 +03:00
# ("", ["", "", ""]),
]
2021-07-08 09:07:29 +03:00
pt = ' .. \ .. \ .. '
2021-07-08 02:08:19 +03:00
def fix_va_string ( file ) :
2021-07-08 09:07:29 +03:00
print " File to process: %s " % file
with open ( file ) as d :
x = d . read ( )
if " #ARGS " in x : #Already patched
print " Already patched "
return
x = x . replace ( " ... " , " ARGS... " )
x = x . replace ( " __VA_ARGS__ " , " #ARGS " )
with open ( file , ' w ' ) as f :
2021-07-08 02:08:19 +03:00
f . write ( x )
2021-07-08 09:07:29 +03:00
def find_file ( folder , files ) :
2021-07-08 02:08:19 +03:00
result = [ ]
2021-07-08 09:07:29 +03:00
directory = os . path . join ( pt , folder )
for dirpath , dirnames , filenames in os . walk ( directory ) :
for filename in filenames :
if filename in files :
result . append ( os . path . join ( dirpath , filename ) )
2021-07-08 02:08:19 +03:00
return result
def fix_va_macro ( folder , file ) :
files = find_file ( folder , file )
2021-07-08 09:07:29 +03:00
if not files :
return
2021-07-08 02:08:19 +03:00
[ fix_va_string ( x ) for x in files ]
def fix_src ( ) :
2021-07-08 09:07:29 +03:00
d = defdict ( list )
for k , v in va_macro_src :
d [ k ] . append ( v )
[ fix_va_macro ( folder , file ) for folder , file in d . iteritems ( ) ]
2021-07-12 11:12:39 +03:00
one_line_patch ( )
def apply_patch ( file , val ) :
print " File to process: %s \n " % file
with open ( file ) as d :
x = d . read ( )
if val [ - 1 ] in x : #Already patched
print " Already patched "
return
x = x . replace ( val [ 1 ] , val [ - 1 ] )
with open ( file , ' w ' ) as f :
f . write ( x )
def patch_line ( file , val ) :
val = val [ 0 ] #get nested list
folder = val [ 0 ]
if not folder :
raise " Prent folder for file %s not set! " % file
files = find_file ( folder , file )
if not files :
return
[ apply_patch ( x , val ) for x in files ]
def one_line_patch ( ) :
d = defdict ( list )
for k , v in oneline_patch_src :
d [ k ] . append ( v )
[ patch_line ( file , val ) for file , val in d . iteritems ( ) ]
2021-07-08 02:08:19 +03:00
2021-07-08 09:07:29 +03:00
if __name__ == ' __main__ ' :
2021-07-12 11:12:39 +03:00
fix_src ( )