最近刷腾讯云开发者社区,看到了腾讯混元大模型,忍不住心痒,想要体验一下。但是打开时,却又不知道要用来做什么。
刚好最近在看iOS代码混淆,遇到了一个问题,即生成无用代码的脚本,网上查到的加入无用代码的逻辑都是OC 的脚本,而想要实现的项目是 Swift 的,所以需要针对 Swift 的项目自己用Python脚本实现,但是 Python不太熟练,所以迟迟无法动手。
于是就想到了,能不能通过腾讯混元大模型来生成?说干就干,首先如果想通过腾讯混元大模型来生成,就需要有一个基本的思路,即条理清楚的告诉它要做什么。所以回过头来整理思路。
根据了解的资料OC的无用代码大致逻辑是,找到.h 和.m 文件中的结束标识 @end,然后在结束标识前,如果是.h 的类,就添加随机属性,如果是.m 的类,就添加随机方法。
所以对于 Swift 的逻辑也是找到某个标识,在标识的位置添加随机的属性和方法。但是由于 Swift 中没有类似 OC 中@end 的标识,所以第一步是找到 Swift 文件的统一标识。想到了所有的 Swift 类都是struct 或 class,所以可以通过判断 struct或 class 开头判断出文件的开始位置,这样就找到了标识,然后再标识的下方插入随机的属性和随机的方法,随机方法中给随机属性赋值,然后在对应的必掉方法中调用随机方法。花费了一番功夫,最终整理出的Swift添加无用代码的步骤如下:
1. 查找class 或 struct 的开头,在下面一行插入随机属性和随机方法
a. 随机方法中,调用随机属性,赋值或其他操作
b. 遍历类如果有 super.viewDidLoad()或者 super.init(frame: frame),则在下一行插入随机方法的调用
然后打开腾讯混元助手,输入上方的步骤,回车,如下图所示:
生成完整代码如下:
import random
import re
# 随机生成属性
def random_attribute():
types = ['Int', 'String', 'Bool', 'Float', 'Double']
type = random.choice(types)
name = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=5))
return f'var {name}: {type}?'
# 随机生成方法
def random_method():
names = ['doSomething', 'calculate', 'update', 'initialize', 'configure']
name = random.choice(names)
return f'func {name}() {{}}'
# 随机生成调用方法的代码
def random_method_call():
names = ['doSomething', 'calculate', 'update', 'initialize', 'configure']
name = random.choice(names)
return f'\t\t{name}()'
# 随机生成调用属性的代码
def random_attribute_call():
names = ['myVar', 'anotherVar', 'thirdVar', 'fourthVar', 'fifthVar']
name = random.choice(names)
return f'\t\t{name} = 0'
# 在 Swift 文件中插入随机属性和随机方法
def insert_random_attribute_and_method(file_path):
with open(file_path, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if re.match(r'\s*class\s+|\s*struct\s+', line):
# 在下一行插入随机属性
lines.insert(i+1, f'\t{random_attribute()}\n')
# 在下一行插入随机方法
lines.insert(i+2, f'\t{random_method()}\n')
# 在随机方法中调用随机属性
if random.random() < 0.5:
lines.insert(i+3, f'\t\t{random_attribute_call()}\n')
# 在类中调用随机方法
if 'super.viewDidLoad()' in line or 'super.init(frame: frame)' in line:
lines.insert(i+4, f'\t\t{random_method_call()}\n')
with open(file_path, 'w') as f:
f.writelines(lines)
# 测试
insert_random_attribute_and_method('test.swift')
新建测试项目,指定要处理的类,未运行脚本前如下图所示,期望是在 class RandomVC: UIViewController 下一行添加随机属性,然后再添加随机方法,随机方法中给随机属性赋值,然后在 viewDidLoad 中调用随机方法。
运行脚本后,可以看到,随机属性生成了,随机方法也生成了,但是随机属性的赋值并没有在随机方法中,且赋值的随机属性和生成的随机属性毫无关系;如下图:
所以再次优化描述,修改“随机方法中,调用随机属性,赋值或其他操作”为“在随机方法中,赋值刚刚生成的随机属性”,如下:
生成的完整代码如下:
import random
import re
# 随机生成属性
def random_attribute():
types = ['Int', 'String', 'Bool', 'Float', 'Double']
type = random.choice(types)
name = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=5))
return f'var {name}: {type}?'
# 随机生成方法
def random_method(attribute_name):
names = ['doSomething', 'calculate', 'update', 'initialize', 'configure']
name = random.choice(names)
return f'func {name}() {{ {attribute_name} = 0 }}'
# 随机生成调用方法的代码
def random_method_call(method_name):
return f'\t\t{method_name}()'
# 在 Swift 文件中插入随机属性和随机方法
def insert_random_attribute_and_method(file_path):
with open(file_path, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if re.match(r'\s*class\s+|\s*struct\s+', line):
# 在下一行插入随机属性
attribute = random_attribute()
lines.insert(i+1, f'\t{attribute}\n')
# 在下一行插入随机方法
method = random_method(attribute.split(' ')[0])
lines.insert(i+2, f'\t{method}\n')
# 在类中调用随机方法
if 'super.viewDidLoad()' in line or 'super.init(frame: frame)' in line:
method_name = method.split(' ')[1].split('(')[0]
lines.insert(i+3, f'\t\t{random_method_call(method_name)}\n')
with open(file_path, 'w') as f:
f.writelines(lines)
# 测试
insert_random_attribute_and_method('test.swift')
再次运行,可以看到,随机属性的赋值在随机方法中,但是赋值的随机属性并不是我们期望的随机属性,且赋值的类型和定义的类型不同,且 viewDidLoad 中没有调用生成的随机方法,如下图:
再次修改描述,把“在随机方法中,赋值刚刚生成的随机属性”改为“在随机方法中,根据Swift 属性的定义获取刚刚生成的随机属性,根据获取的随机属性的不同类型赋值对应类型的值”,同时把“遍历类如果有..”改为“遍历类的每一行,如果有...”,如下图:
生成的完整代码如下:
import random
import re
# 随机生成属性
def random_attribute():
types = ['Int', 'String', 'Bool', 'Float', 'Double']
type = random.choice(types)
name = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=5))
return f'var {name}: {type}?'
# 随机生成方法
def random_method(attribute_name, attribute_type):
names = ['doSomething', 'calculate', 'update', 'initialize', 'configure']
name = random.choice(names)
value = random_value(attribute_type)
return f'func {name}() {{ {attribute_name} = {value} }}'
# 随机生成值
def random_value(attribute_type):
if attribute_type == 'Int':
return random.randint(0, 100)
elif attribute_type == 'String':
return '"' + ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=5)) + '"'
elif attribute_type == 'Bool':
return random.choice([True, False])
elif attribute_type == 'Float':
return round(random.uniform(0, 1), 2)
elif attribute_type == 'Double':
return round(random.uniform(0, 1), 2)
# 随机生成调用方法的代码
def random_method_call(method_name):
return f'\t\t{method_name}()'
# 在 Swift 文件中插入随机属性和随机方法
def insert_random_attribute_and_method(file_path):
with open(file_path, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if re.match(r'\s*class\s+|\s*struct\s+', line):
# 在下一行插入随机属性
attribute = random_attribute()
lines.insert(i+1, f'\t{attribute}\n')
# 在下一行插入随机方法
attribute_name, attribute_type = attribute.split(':')[0].split(' ')
method = random_method(attribute_name, attribute_type)
lines.insert(i+2, f'\t{method}\n')
# 在类中调用随机方法
for j in range(i+3, len(lines)):
if 'super.viewDidLoad()' in lines[j] or 'super.init(frame: frame)' in lines[j]:
method_name = method.split(' ')[1].split('(')[0]
lines.insert(j+1, f'\t\t{random_method_call(method_name)}\n')
with open(file_path, 'w') as f:
f.writelines(lines)
# 测试
insert_random_attribute_and_method('test.swift')
运行后,发现获取的属性和赋值还不正确,但是随机方法的调用已经添加,如下图所示:
通过查看上面生成的代码可以看到,错误的原因还是因为截取的属性不对,从而导致属性类型也没有获取到。到这一步,感觉描述已经很清楚,但是生成的还是不对,以为只能自己手动修改了,突然灵机一动,在输入框输入:“上面代码中获取生成的随机属性不对,换一种方法实现”,如下图:
完整代码如下:
import random
import re
# 随机生成属性
def random_attribute():
types = ['Int', 'String', 'Bool', 'Float', 'Double']
type = random.choice(types)
name = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=5))
return f'var {name}: {type}?', {name: type}
# 随机生成方法
def random_method(attribute_name, attribute_type):
names = ['doSomething', 'calculate', 'update', 'initialize', 'configure']
name = random.choice(names)
value = random_value(attribute_type)
return f'func {name}() {{ {attribute_name} = {value} }}'
# 随机生成值
def random_value(attribute_type):
if attribute_type == 'Int':
return random.randint(0, 100)
elif attribute_type == 'String':
return '"' + ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=5)) + '"'
elif attribute_type == 'Bool':
return random.choice([True, False])
elif attribute_type == 'Float':
return round(random.uniform(0, 1), 2)
elif attribute_type == 'Double':
return round(random.uniform(0, 1), 2)
# 随机生成调用方法的代码
def random_method_call(method_name):
return f'\t\t{method_name}()'
# 在 Swift 文件中插入随机属性和随机方法
def insert_random_attribute_and_method(file_path):
with open(file_path, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if re.match(r'\s*class\s+|\s*struct\s+', line):
# 在下一行插入随机属性
attribute, attribute_dict = random_attribute()
lines.insert(i+1, f'\t{attribute}\n')
# 在下一行插入随机方法
attribute_name, attribute_type = next(iter(attribute_dict.items()))
method = random_method(attribute_name, attribute_type)
lines.insert(i+2, f'\t{method}\n')
# 在类中调用随机方法
for j in range(i+3, len(lines)):
if 'super.viewDidLoad()' in lines[j] or 'super.init(frame: frame)' in lines[j]:
method_name = method.split(' ')[1].split('(')[0]
lines.insert(j+1, f'\t\t{random_method_call(method_name)}\n')
with open(file_path, 'w') as f:
f.writelines(lines)
# 测试
insert_random_attribute_and_method('test.swift')
运行后,效果如下:
可以看到,完美实现了需求,生成随机属性和随机方法,赋值随机属性,调用随机方法。如果感觉调用方法的地方多了一个缩进不完美,还可以继续跟它说:“上面调用随机代码的地方多了一个缩进,重新生成一下”,如下图:
再次运行,结果就完美无缺了。如下图:
别急,上面是针对单个 swift 文件的代码,再让它写一个批量处理的代码,如下图:
运行后,会发现项目中所有.swfit 的文件都经过了处理,Perfect!
写在最后,本次体验腾讯混元助手真的很完美,虽然几经波折,但最终自己没有写一行代码,只告诉它要做什么,它自己会去找如何做;如果结果的不对,指出它错了,让它换种方法实现,它就真的能照做,好像能听懂似的,这种感觉很惊喜,像跟人沟通一样,很智能。总结起来就是:真的很赞很强大。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。