|
24 | 24 | # |
25 | 25 | # ##### END MIT LICENSE BLOCK ##### |
26 | 26 |
|
27 | | -import io |
28 | | -import struct |
29 | | - |
30 | | -from enum import Flag, Enum, auto |
31 | | - |
32 | | -class FunctionTypeEnum(Enum): |
33 | | - identity = 0 |
34 | | - constant = auto() |
35 | | - transition = auto() |
36 | | - periodic = auto() |
37 | | - linear = auto() |
38 | | - linear_key = auto() |
39 | | - multi_linear_key = auto() |
40 | | - spline = auto() |
41 | | - multi_spline = auto() |
42 | | - exponent = auto() |
43 | | - spline2 = auto() |
44 | | - |
45 | | -class MappingFlags(Flag): |
46 | | - scalar_intensity = 0 |
47 | | - _range = 1 |
48 | | - constant = 16 |
49 | | - _2_color = 32 |
50 | | - _3_color = 48 |
51 | | - _4_color = 64 |
52 | | - |
53 | | -def read_byte(function_stream, endian_override): |
54 | | - struct_string = '%sb' % endian_override |
55 | | - return (struct.unpack(struct_string, function_stream.read(1)))[0] |
56 | | - |
57 | | -def read_real(function_stream, endian_override): |
58 | | - struct_string = '%sf' % endian_override |
59 | | - return (struct.unpack(struct_string, function_stream.read(4)))[0] |
60 | | - |
61 | | -def read_bgra(function_stream, endian_override): |
62 | | - struct_string = '%s4B' % endian_override |
63 | | - a, r, g, b = struct.unpack(struct_string, function_stream.read(4))[::-1] |
64 | | - return {"A": a/255,"R": r/255, "G": g/255, "B": b/255} |
65 | | - |
66 | | -def read_real_point_2d(function_stream, endian_override): |
67 | | - struct_string = '%s2f' % endian_override |
68 | | - return (struct.unpack(struct_string, function_stream.read(8))) |
69 | | - |
70 | | -def write_real(function_stream, endian_override, value): |
71 | | - struct_string = '%sf' % endian_override |
72 | | - function_stream.write(struct.pack(struct_string, value)) |
73 | | - |
74 | | -def write_bgra(function_stream, endian_override, argb): |
75 | | - struct_string = '%s4B' % endian_override |
76 | | - function_stream.write(struct.pack(struct_string, *reversed(argb.values()))) |
77 | | - |
78 | | -def write_real_point_2d(function_stream, endian_override, value): |
79 | | - struct_string = '%s2f' % endian_override |
80 | | - function_stream.write(struct.pack(struct_string, *value)) |
81 | | - |
82 | | -def normalize_list(value, size, default=0): |
83 | | - if not isinstance(value, list): |
84 | | - return [default] * size |
85 | | - |
86 | | - return (value + [default] * size)[:size] |
87 | | - |
88 | | -def create_function(function_type=0, flags=0, function_1=0, function_2=0, mapping_inputs=[], function_inputs=[]): |
89 | | - data_field = [] |
90 | | - endian_override = "<" |
91 | | - function_stream = io.BytesIO() |
92 | | - function_stream.write(struct.pack('%sb' % endian_override, function_type)) |
93 | | - function_stream.write(struct.pack('%sb' % endian_override, flags)) |
94 | | - function_stream.write(struct.pack('%sb' % endian_override, function_1)) |
95 | | - function_stream.write(struct.pack('%sb' % endian_override, function_2)) |
96 | | - mapping_flags = MappingFlags(flags) |
97 | | - if MappingFlags._2_color in mapping_flags: |
98 | | - color_inputs = normalize_list(mapping_inputs, 2, (0, 0, 0, 0)) |
99 | | - write_bgra(function_stream, endian_override, color_inputs[0]) # Color A |
100 | | - function_stream.write(bytes(8)) |
101 | | - write_bgra(function_stream, endian_override, color_inputs[1]) # Color B |
102 | | - |
103 | | - elif MappingFlags._3_color in mapping_flags: |
104 | | - color_inputs = normalize_list(mapping_inputs, 3, (0, 0, 0, 0)) |
105 | | - write_bgra(function_stream, endian_override, color_inputs[0]) # Color A |
106 | | - write_bgra(function_stream, endian_override, color_inputs[1]) # Color B |
107 | | - function_stream.write(bytes(4)) |
108 | | - write_bgra(function_stream, endian_override, color_inputs[2]) # Color C |
109 | | - |
110 | | - elif MappingFlags._4_color in mapping_flags: |
111 | | - color_inputs = normalize_list(mapping_inputs, 4, (0, 0, 0, 0)) |
112 | | - write_bgra(function_stream, endian_override, color_inputs[0]) # Color A |
113 | | - write_bgra(function_stream, endian_override, color_inputs[1]) # Color B |
114 | | - write_bgra(function_stream, endian_override, color_inputs[2]) # Color C |
115 | | - write_bgra(function_stream, endian_override, color_inputs[4]) # Color D |
116 | | - |
117 | | - else: |
118 | | - float_inputs = normalize_list(mapping_inputs, 2, 0.0) |
119 | | - write_real(function_stream, endian_override, float_inputs[0]) # Lower Bound |
120 | | - write_real(function_stream, endian_override, float_inputs[1]) # Upper Bound |
121 | | - function_stream.write(bytes(8)) |
122 | | - |
123 | | - if FunctionTypeEnum.constant == FunctionTypeEnum(function_type): |
124 | | - function_stream.write(bytes(8)) |
125 | | - |
126 | | - elif FunctionTypeEnum.transition == FunctionTypeEnum(function_type): |
127 | | - float_inputs = normalize_list(function_inputs, 4, 0.0) |
128 | | - write_real(function_stream, endian_override, float_inputs[0]) # Function Min |
129 | | - write_real(function_stream, endian_override, float_inputs[1]) # Function Max |
130 | | - write_real(function_stream, endian_override, float_inputs[2]) # Range Function Min |
131 | | - write_real(function_stream, endian_override, float_inputs[3]) # Range Function Max |
132 | | - |
133 | | - elif FunctionTypeEnum.periodic == FunctionTypeEnum(function_type): |
134 | | - float_inputs = normalize_list(function_inputs, 8, 0.0) |
135 | | - write_real(function_stream, endian_override, float_inputs[0]) # Frequency |
136 | | - write_real(function_stream, endian_override, float_inputs[1]) # Phase |
137 | | - write_real(function_stream, endian_override, float_inputs[2]) # Function Min |
138 | | - write_real(function_stream, endian_override, float_inputs[3]) # Function Max |
139 | | - write_real(function_stream, endian_override, float_inputs[4]) # Range Frequency |
140 | | - write_real(function_stream, endian_override, float_inputs[5]) # Range Phase |
141 | | - write_real(function_stream, endian_override, float_inputs[6]) # Range Function Min |
142 | | - write_real(function_stream, endian_override, float_inputs[7]) # Range Function Max |
143 | | - |
144 | | - elif FunctionTypeEnum.linear == FunctionTypeEnum(function_type): |
145 | | - point_inputs = normalize_list(function_inputs, 4, (0.0, 0.0)) |
146 | | - for point_input in point_inputs[0:2]: |
147 | | - write_real_point_2d(function_stream, endian_override, point_input) |
148 | | - |
149 | | - function_stream.write(bytes(8)) |
150 | | - for point_input in point_inputs[2:4]: |
151 | | - write_real_point_2d(function_stream, endian_override, point_input) |
152 | | - |
153 | | - function_stream.write(bytes(8)) |
154 | | - |
155 | | - elif FunctionTypeEnum.linear_key == FunctionTypeEnum(function_type): |
156 | | - point_inputs = normalize_list(function_inputs, 8, (0.0, 0.0)) |
157 | | - for point_input in point_inputs[0:4]: |
158 | | - write_real_point_2d(function_stream, endian_override, point_input) |
159 | | - |
160 | | - function_stream.write(bytes(48)) |
161 | | - for point_input in point_inputs[4:8]: |
162 | | - write_real_point_2d(function_stream, endian_override, point_input) |
163 | | - |
164 | | - function_stream.write(bytes(48)) |
165 | | - |
166 | | - elif FunctionTypeEnum.multi_linear_key == FunctionTypeEnum(function_type): |
167 | | - function_stream.write(bytes(256)) |
168 | | - |
169 | | - elif FunctionTypeEnum.spline == FunctionTypeEnum(function_type): |
170 | | - point_inputs = normalize_list(function_inputs, 8, (0.0, 0.0)) |
171 | | - for point_input in point_inputs[0:4]: |
172 | | - write_real_point_2d(function_stream, endian_override, point_input) |
173 | | - |
174 | | - function_stream.write(bytes(16)) |
175 | | - for point_input in point_inputs[4:8]: |
176 | | - write_real_point_2d(function_stream, endian_override, point_input) |
177 | | - |
178 | | - function_stream.write(bytes(16)) |
179 | | - |
180 | | - elif FunctionTypeEnum.multi_spline == FunctionTypeEnum(function_type): |
181 | | - function_stream.write(bytes(40)) |
182 | | - |
183 | | - elif FunctionTypeEnum.exponent == FunctionTypeEnum(function_type): |
184 | | - float_inputs = normalize_list(function_inputs, 6, 0.0) |
185 | | - write_real(function_stream, endian_override, float_inputs[0]) # Function Min |
186 | | - write_real(function_stream, endian_override, float_inputs[1]) # Function Max |
187 | | - write_real(function_stream, endian_override, float_inputs[2]) # Exponent |
188 | | - write_real(function_stream, endian_override, float_inputs[3]) # Range Function Min |
189 | | - write_real(function_stream, endian_override, float_inputs[4]) # Range Function Max |
190 | | - write_real(function_stream, endian_override, float_inputs[5]) # Range Exponent |
191 | | - |
192 | | - elif FunctionTypeEnum.spline2 == FunctionTypeEnum(function_type): |
193 | | - point_inputs = normalize_list(function_inputs, 8, (0.0, 0.0)) |
194 | | - for point_input in point_inputs[0:4]: |
195 | | - write_real_point_2d(function_stream, endian_override, point_input) |
196 | | - |
197 | | - function_stream.write(bytes(16)) |
198 | | - for point_input in point_inputs[4:8]: |
199 | | - write_real_point_2d(function_stream, endian_override, point_input) |
200 | | - |
201 | | - function_stream.write(bytes(16)) |
202 | | - |
203 | | - for byte in function_stream.getbuffer(): |
204 | | - signed_byte = byte if byte < 128 else byte - 256 |
205 | | - data_field.append({"Value": signed_byte}) |
206 | | - |
207 | | - return data_field |
| 27 | +from .functions import create_function, FunctionTypeEnum |
208 | 28 |
|
209 | 29 | def generate_player_responses(dump_dic): |
210 | 30 | player_responses_block = [] |
|
0 commit comments