2019-08-02 08:17:32 +02:00
|
|
|
tool
|
2019-07-31 20:53:00 +02:00
|
|
|
extends Object
|
|
|
|
class_name MMGenLoader
|
|
|
|
|
2019-10-07 20:41:57 +02:00
|
|
|
const STD_GENDEF_PATH = "res://addons/material_maker/nodes"
|
|
|
|
|
2019-10-12 07:10:25 +02:00
|
|
|
static func generator_name_from_path(path : String) -> String:
|
|
|
|
for p in [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]:
|
|
|
|
print(p)
|
|
|
|
print(path.get_base_dir())
|
|
|
|
return path.get_basename().get_file()
|
|
|
|
|
2019-09-09 22:00:18 +02:00
|
|
|
static func load_gen(filename: String) -> MMGenBase:
|
2019-07-31 20:53:00 +02:00
|
|
|
var file = File.new()
|
|
|
|
if file.open(filename, File.READ) == OK:
|
|
|
|
var data = parse_json(file.get_as_text())
|
|
|
|
return create_gen(data)
|
|
|
|
return null
|
|
|
|
|
2019-09-09 22:00:18 +02:00
|
|
|
static func add_to_gen_graph(gen_graph, generators, connections):
|
2019-08-15 11:53:21 +02:00
|
|
|
var rv = { generators=[], connections=[] }
|
2019-09-17 08:12:53 +02:00
|
|
|
var gennames = {}
|
2019-08-15 11:53:21 +02:00
|
|
|
for n in generators:
|
|
|
|
var g = create_gen(n)
|
|
|
|
if g != null:
|
2019-09-17 08:12:53 +02:00
|
|
|
var orig_name = g.name
|
2019-09-29 10:07:32 +02:00
|
|
|
gen_graph.add_generator(g)
|
2019-08-15 11:53:21 +02:00
|
|
|
rv.generators.append(g)
|
2019-09-29 10:07:32 +02:00
|
|
|
gennames[orig_name] = g.name
|
2019-08-15 11:53:21 +02:00
|
|
|
for c in connections:
|
2019-09-17 08:12:53 +02:00
|
|
|
if gennames.has(c.from) and gennames.has(c.to):
|
|
|
|
c.from = gennames[c.from]
|
|
|
|
c.to = gennames[c.to]
|
|
|
|
gen_graph.connections.append(c)
|
|
|
|
rv.connections.append(c)
|
2019-08-15 11:53:21 +02:00
|
|
|
return rv
|
|
|
|
|
2019-09-09 22:00:18 +02:00
|
|
|
static func create_gen(data) -> MMGenBase:
|
2019-10-07 20:41:57 +02:00
|
|
|
var guess = [
|
|
|
|
{ keyword="connections", type=MMGenGraph },
|
|
|
|
{ keyword="nodes", type=MMGenGraph },
|
|
|
|
{ keyword="shader_model", type=MMGenShader },
|
|
|
|
{ keyword="model_data", type=MMGenShader },
|
|
|
|
{ keyword="convolution_params", type=MMGenConvolution },
|
|
|
|
{ keyword="widgets", type=MMGenRemote }
|
|
|
|
]
|
|
|
|
var types = {
|
|
|
|
material = MMGenMaterial,
|
|
|
|
buffer = MMGenBuffer,
|
|
|
|
image = MMGenImage,
|
|
|
|
switch = MMGenSwitch
|
|
|
|
}
|
2019-07-31 20:53:00 +02:00
|
|
|
var generator = null
|
|
|
|
if data.has("connections") and data.has("nodes"):
|
|
|
|
generator = MMGenGraph.new()
|
2019-09-17 08:12:53 +02:00
|
|
|
if data.has("label"):
|
|
|
|
generator.label = data.label
|
2019-08-15 11:53:21 +02:00
|
|
|
add_to_gen_graph(generator, data.nodes, data.connections)
|
2019-08-17 17:35:48 +02:00
|
|
|
elif data.has("shader_model"):
|
2019-08-12 15:58:42 +02:00
|
|
|
generator = MMGenShader.new()
|
2019-08-17 17:35:48 +02:00
|
|
|
generator.set_shader_model(data.shader_model)
|
2019-09-16 20:45:47 +02:00
|
|
|
elif data.has("convolution_params"):
|
|
|
|
generator = MMGenConvolution.new()
|
|
|
|
generator.set_convolution_params(data.convolution_params)
|
2019-09-09 22:00:18 +02:00
|
|
|
elif data.has("model_data"):
|
|
|
|
generator = MMGenShader.new()
|
|
|
|
generator.set_shader_model(data.model_data)
|
2019-09-20 20:43:57 +02:00
|
|
|
elif data.has("widgets"):
|
|
|
|
generator = MMGenRemote.new()
|
2019-09-26 22:32:49 +02:00
|
|
|
generator.set_widgets(data.widgets.duplicate(true))
|
2019-07-31 20:53:00 +02:00
|
|
|
elif data.has("type"):
|
|
|
|
if data.type == "material":
|
|
|
|
generator = MMGenMaterial.new()
|
2019-08-17 17:35:48 +02:00
|
|
|
elif data.type == "buffer":
|
|
|
|
generator = MMGenBuffer.new()
|
2019-08-25 23:27:07 +02:00
|
|
|
elif data.type == "image":
|
|
|
|
generator = MMGenImage.new()
|
2019-09-17 08:12:53 +02:00
|
|
|
elif data.type == "ios":
|
|
|
|
generator = MMGenIOs.new()
|
|
|
|
generator.ports = data.ports
|
2019-09-22 22:17:26 +02:00
|
|
|
elif data.type == "switch":
|
|
|
|
generator = MMGenSwitch.new()
|
2019-07-31 20:53:00 +02:00
|
|
|
else:
|
2019-08-12 15:58:42 +02:00
|
|
|
var file = File.new()
|
2019-10-07 20:41:57 +02:00
|
|
|
var gen_paths = [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]
|
|
|
|
for p in gen_paths:
|
|
|
|
if file.open(p+"/"+data.type+".mmg", File.READ) == OK:
|
|
|
|
generator = create_gen(parse_json(file.get_as_text()))
|
|
|
|
generator.model = data.type
|
|
|
|
file.close()
|
|
|
|
break
|
|
|
|
elif file.open(p+"/"+data.type+".mmn", File.READ) == OK:
|
|
|
|
generator = MMGenShader.new()
|
|
|
|
generator.model = data.type
|
|
|
|
generator.set_shader_model(parse_json(file.get_as_text()))
|
|
|
|
file.close()
|
|
|
|
break
|
|
|
|
if generator == null:
|
2019-08-12 15:58:42 +02:00
|
|
|
print("Cannot find description for "+data.type)
|
2019-08-17 17:35:48 +02:00
|
|
|
if generator != null:
|
|
|
|
generator.name = data.type
|
2019-08-09 08:16:24 +02:00
|
|
|
else:
|
2019-09-09 22:00:18 +02:00
|
|
|
print("LOADER: data not supported:"+str(data))
|
2019-08-12 15:58:42 +02:00
|
|
|
if generator != null:
|
2019-08-15 11:53:21 +02:00
|
|
|
if data.has("name"):
|
|
|
|
generator.name = data.name
|
2019-08-12 15:58:42 +02:00
|
|
|
if data.has("node_position"):
|
|
|
|
generator.position.x = data.node_position.x
|
|
|
|
generator.position.y = data.node_position.y
|
|
|
|
if data.has("parameters"):
|
2019-08-17 17:35:48 +02:00
|
|
|
for p in data.parameters.keys():
|
2019-09-09 22:00:18 +02:00
|
|
|
generator.parameters[p] = MMType.deserialize_value(data.parameters[p])
|
2019-10-12 07:10:25 +02:00
|
|
|
else:
|
|
|
|
for p in generator.get_parameter_defs():
|
|
|
|
if data.has(p.name):
|
|
|
|
generator.parameters[p.name] = MMType.deserialize_value(data[p.name])
|
2019-07-31 20:53:00 +02:00
|
|
|
return generator
|
2019-10-07 20:41:57 +02:00
|
|
|
|
|
|
|
static func get_generator_list() -> Array:
|
|
|
|
var rv = []
|
|
|
|
var dir : Directory = Directory.new()
|
|
|
|
for p in [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]:
|
|
|
|
dir.open(p)
|
|
|
|
dir.list_dir_begin(true)
|
|
|
|
while true:
|
|
|
|
var f = dir.get_next()
|
|
|
|
if f == "":
|
|
|
|
break
|
|
|
|
if f.right(f.length()-4) == ".mmg":
|
|
|
|
var n = f.left(f.length()-4)
|
|
|
|
if rv.find(n) == -1:
|
|
|
|
rv.push_back(n)
|
|
|
|
rv.sort()
|
|
|
|
return rv
|