indexing description: "[ EiffelMedia Resource Generator ]" date: "$Date$" revision: "$Revision$" class EM_RESOURCE_GENERATOR inherit EM_CLASS_GENERATOR create make feature -- Initialization generate_classes is -- Generate classes and write them to disc do io.put_string ("Generating resource loading class...%N") generate_main_class if not has_error and not no_singleton then io.put_string ("Generating shared resources class...%N") generate_shared_class end if not has_error then io.put_string ("Writing " + main_class_name + ".e...%N") write_main_file end if not has_error and not no_singleton then io.put_string ("Writing " + shared_class_name + ".e...%N") write_shared_file end if not has_error then io.put_string ("Success%N") end end generate_main_class is -- Generate the Main class do create main_class_content.make_empty main_class_content.append_string (header_code) main_class_content.append_string (make_code) main_class_content.append_string (attributes_code) main_class_content.append_string (path_code) main_class_content.append_string (load_resources_code) main_class_content.append_string (load_next_resource_code) main_class_content.append_string (start_loading_resources_code) main_class_content.append_string (free_resources_code) main_class_content.append_string (error_handling_code) main_class_content.append_string (getter_code) main_class_content.append_string (footer_code) end generate_shared_class is -- Generate class with the singleton do create shared_class_content.make_empty shared_class_content.append_string ("indexing%N%N") shared_class_content.append_string ("%Tdescription: %"[%N%N") shared_class_content.append_string (do_not_edit_message) shared_class_content.append_string ("%N%N") shared_class_content.append_string ("%T%TResources singleton%N%N") shared_class_content.append_string ("%T]%"%N") shared_class_content.append_string ("%Tdate: %"$Date$%"%N") shared_class_content.append_string ("%Trevision: %"$Revision$%"") shared_class_content.append_string ("%N%N") shared_class_content.append_string ("class%N") shared_class_content.append_string ("%T" + shared_class_name.as_upper + "%N%N") shared_class_content.append_string ("feature%N") shared_class_content.append_string ("%T" + main_class_name.as_lower + ": " + main_class_name.as_upper + " is%N") shared_class_content.append_string("%T%T%T-- Shared resources%N") shared_class_content.append_string ("%T%Tindexing%N") shared_class_content.append_string ("%T%T%Tonce_status: global%N") shared_class_content.append_string ("%T%Tonce%N") shared_class_content.append_string ("%T%T%Tcreate Result.make%N") shared_class_content.append_string("%T%Tend%N%N") shared_class_content.append_string ("end%N") end feature -- Code Generators header_code: STRING is require not has_error do create Result.make_empty Result.append_string ("indexing%N%N") Result.append_string (" description: %"[%N%N") Result.append_string (do_not_edit_message) Result.append_string ("%N%N") Result.append_string ("%T%TClass to load and access resources%N%N") Result.append_string ("%T]%"%N") Result.append_string ("%Tdate: %"$Date$%"%N") Result.append_string ("%Trevision: %"$Revision$%"") Result.append_string ("%N%N") Result.append_string ("class%N") Result.append_string ("%T" + main_class_name.as_upper + "%N%N") -- Inherit Result.append_string("inherit%N") -- File loader Result.append_string("%TEM_SHARED_FILE_LOADER%N") Result.append_string("%T%Texport {NONE} all end%N%N") Result.append_string ("creation%N") Result.append_string ("%Tmake%N%N") end make_code: STRING is require not has_error do create Result.make_empty Result.append_string ("feature -- Initialization%N%N") Result.append_string("%Tmake is%N") Result.append_string("%T%Tdo%N") Result.append_string("%T%T%Treset_error%N") Result.append_string("%T%T%Tcreate error_files.make%N") Result.append_string("%T%T%Tfile_loader.set_ignore_missing_file (true)%N") Result.append_string("%T%Tend%N%N") end attributes_code: STRING is require not has_error xml_table /= void local num_of_all_resources: INTEGER current_entry: DS_HASH_TABLE[STRING, STRING] a_type: STRING do create Result.make_empty -- Attributes Result.append_string ("feature -- Attributes%N%N") from xml_table.start until xml_table.after loop Result.append_string("%T-- Resources for Section " + xml_table.key_for_iteration.as_lower + "%N%N") from xml_table.item_for_iteration.start until xml_table.item_for_iteration.after loop current_entry := xml_table.item_for_iteration.item_for_iteration check_for_attributes (<<"name", "type">>, current_entry) if attributes_ok then a_type := xml_table.item_for_iteration.item_for_iteration.item("type").as_lower Result.append_string ("%T" + current_entry.item("name") + ": ") if a_type.is_equal ("animation") then Result.append_string ("EM_ANIMATION%N") elseif a_type.is_equal ("sprite") then Result.append_string ("EM_SPRITE%N") elseif a_type.is_equal ("image") then Result.append_string ("EM_BITMAP%N") elseif a_type.is_equal ("sound") then Result.append_string ("EM_SOUND%N") elseif a_type.is_equal ("music") then Result.append_string ("EM_MUSIC%N") elseif a_type.is_equal ("bmp_font") then Result.append_string ("EM_BMP_FONT%N") elseif a_type.is_equal ("ttf_font") then Result.append_string ("EM_COLOR_TTF_FONT%N") else print_error ("Unknown Resource Type found (" + a_type + ")") end end xml_table.item_for_iteration.forth end Result.append_string ("%N%Tnum_of_" + xml_table.key_for_iteration.as_lower + "_resources: INTEGER is " + xml_table.item_for_iteration.count.out + "%N") num_of_all_resources := num_of_all_resources + xml_table.item_for_iteration.count Result.append_string ("%T" + xml_table.key_for_iteration.as_lower + "_progress: INTEGER%N") Result.append_string ("%T" + xml_table.key_for_iteration.as_lower + "_is_loaded: BOOLEAN%N") xml_table.forth end Result.append_string ("%N%Tnum_of_all_resources: INTEGER is " + (num_of_all_resources).out + "%N") Result.append_string ("%Tall_progress: INTEGER%N") Result.append_string ("%Tall_is_loaded: BOOLEAN%N") Result.append_string ("%N") end path_code: STRING is require not has_error xml_table /= void local current_entry: DS_HASH_TABLE[STRING, STRING] do create Result.make_empty -- Paths Result.append_string ("feature -- Paths%N%N") from xml_table.start until xml_table.after loop Result.append_string("%T-- Paths for Section " + xml_table.key_for_iteration.as_lower + "%N%N") from xml_table.item_for_iteration.start until xml_table.item_for_iteration.after loop current_entry := xml_table.item_for_iteration.item_for_iteration check_for_attributes (<<"name", "path">>, current_entry) if attributes_ok then Result.append_string ("%Tpath_" + current_entry.item ("name") + ": STRING is %"" + current_entry.item("path") + "%"%N") end xml_table.item_for_iteration.forth end xml_table.forth end Result.append_string ("%N") end check_if_loaded_code(a_prefix: STRING) : STRING is require not has_error xml_table /= void local new_cursor: DS_HASH_TABLE_CURSOR[DS_LINKED_LIST [DS_HASH_TABLE [STRING, STRING]], STRING] do new_cursor := xml_table.new_cursor create Result.make_empty Result.append_string (a_prefix + " if ") from new_cursor.start until new_cursor.after loop Result.append_string (xml_table.key_for_iteration.as_lower + "_is_loaded") new_cursor.forth if not new_cursor.after then Result.append_string (" and ") end end Result.append_string (" then %N") Result.append_string (a_prefix + "%Tall_is_loaded := true%N") Result.append_string (a_prefix + "end%N") end load_resources_code: STRING is require not has_error xml_table /= void do create Result.make_empty Result.append_string ("feature -- Load resources sections%N%N") Result.append_string ("%Tload_all is%N") Result.append_string ("%T%T%T-- loading procedure for all resources%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfrom start_loading_all%N") Result.append_string ("%T%T%Tuntil all_is_loaded%N") Result.append_string ("%T%T%Tloop%N") Result.append_string ("%T%T%T%Tload_next_all_resource%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N") Result.append_string("%N") from xml_table.start until xml_table.after loop Result.append_string ("%Tload_"+ xml_table.key_for_iteration.as_lower +" is%N") Result.append_string ("%T%T%T-- loading procedure for "+ xml_table.key_for_iteration.as_lower +" resources%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfrom start_loading_" + xml_table.key_for_iteration.as_lower + "%N") Result.append_string ("%T%T%Tuntil "+ xml_table.key_for_iteration.as_lower + "_is_loaded%N") Result.append_string ("%T%T%Tloop%N") Result.append_string ("%T%T%T%Tload_next_"+ xml_table.key_for_iteration.as_lower + "_resource%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N") Result.append_string("%N") xml_table.forth end end start_loading_resources_code: STRING is require not has_error xml_table /= void do create Result.make_empty Result.append_string ("%Tstart_loading_all is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tall_progress := 0%N") Result.append_string ("%T%T%Tall_is_loaded := false%N") Result.append_string ("%T%Tend%N%N") from xml_table.start until xml_table.after loop Result.append_string ("%Tstart_loading_"+ xml_table.key_for_iteration.as_lower + " is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%T"+ xml_table.key_for_iteration.as_lower + "_progress := 0%N") Result.append_string ("%T%T%T"+ xml_table.key_for_iteration.as_lower + "_is_loaded := false%N") Result.append_string ("%T%Tend%N%N") xml_table.forth end end load_next_resource_code: STRING is require not has_error xml_table /= void local i: INTEGER do create Result.make_empty -- Load all Resources at once Result.append_string ("feature -- Load resources%N%N") Result.append_string ("%Tload_next_all_resource is%N") Result.append_string ("%T%T%T-- loading procedure for all resources%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tinspect%N") Result.append_string ("%T%T%T%Tall_progress%N") from xml_table.start i := 0 until xml_table.after loop from xml_table.item_for_iteration.start until xml_table.item_for_iteration.after loop Result.append_string ("%T%T%Twhen " + i.out + " then%N") Result.append_string(appropriate_getter_code(xml_table.item_for_iteration.item_for_iteration)) i := i + 1 xml_table.item_for_iteration.forth end xml_table.forth end Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%T%Tall_progress := all_progress + 1%N") Result.append_string ("%T%T%Tif all_progress = num_of_all_resources then%N") Result.append_string ("%T%T%T%Tall_is_loaded := true%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N") Result.append_string("%N") -- Loading from xml_table.start until xml_table.after loop Result.append_string ("%Tload_next_" + xml_table.key_for_iteration.as_lower + "_resource is%N") Result.append_string ("%T%T%T-- loading procedure for " + xml_table.key_for_iteration.as_lower + "%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tinspect%N") Result.append_string ("%T%T%T%T" + xml_table.key_for_iteration.as_lower + "_progress%N") from xml_table.item_for_iteration.start i := 0 until xml_table.item_for_iteration.after loop Result.append_string ("%T%T%Twhen " + i.out + " then%N") Result.append_string (appropriate_getter_code (xml_table.item_for_iteration.item_for_iteration)) i := i + 1 xml_table.item_for_iteration.forth end Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%T%T " + xml_table.key_for_iteration.as_lower + "_progress := " + xml_table.key_for_iteration.as_lower + "_progress + 1%N") Result.append_string ("%T%T%Tif " + xml_table.key_for_iteration.as_lower + "_progress = num_of_" + xml_table.key_for_iteration.as_lower + "_resources then%N") Result.append_string ("%T%T%T%T" + xml_table.key_for_iteration.as_lower + "_is_loaded := true%N") Result.append_string (check_if_loaded_code ("%T%T%T%T")) Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend") Result.append_string("%N%N") xml_table.forth end end appropriate_getter_code (an_entry: DS_HASH_TABLE[STRING, STRING]): STRING is require not has_error xml_table /= void local a_type: STRING a_name: STRING do create Result.make_empty a_type := an_entry.item("type") a_name := an_entry.item("name") if a_type.is_equal ("animation") then Result.append_string ("%T%T%T%T" + a_name + " := get_animation (path_" + a_name + ")%N") elseif a_type.is_equal ("sprite") then Result.append_string ("%T%T%T%T" + a_name + " := get_sprite (path_" + a_name + ")%N") elseif a_type.is_equal ("image") then Result.append_string ("%T%T%T%T" + a_name + " := get_bitmap (path_" + a_name + ")%N") elseif a_type.is_equal ("sound") then Result.append_string ("%T%T%T%T" + a_name + " := get_sound (path_" + a_name + ")%N") elseif a_type.is_equal ("music") then Result.append_string ("%T%T%T%T" + a_name + " := get_music (path_" + a_name + ")%N") elseif a_type.is_equal ("bmp_font") then Result.append_string ("%T%T%T%T" + a_name + " := get_bmp_font (path_" + a_name + ")%N") elseif a_type.is_equal ("ttf_font") then if an_entry.has ("point_size") then Result.append_string ("%T%T%T%T" + a_name + " := get_ttf_font (path_" + a_name + ", " + an_entry.item("point_size") + ")%N") else Result.append_string ("%T%T%T%T" + a_name + " := get_ttf_font (path_" + a_name + ", " + default_point_size.out + ")%N") end else print_error ("Unknown Resource Type found (" + a_type + ")") end end free_resources_code: STRING is require not has_error xml_table /= void do create Result.make_empty -- Free all Resources at once Result.append_string ("feature -- Free resources%N%N") Result.append_string ("%Tfree_all is%N") Result.append_string ("%T%T%T-- freeing procedure for all resources%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tall_progress := 0%N") Result.append_string ("%T%T%Tall_is_loaded := false%N") from xml_table.start until xml_table.after loop Result.append_string ("%T%T%Tfree_" + xml_table.key_for_iteration.as_lower + "%N") xml_table.forth end Result.append_string ("%T%Tend") Result.append_string("%N%N") -- Free Resources from xml_table.start until xml_table.after loop Result.append_string ("%Tfree_" + xml_table.key_for_iteration.as_lower + " is%N") Result.append_string ("%T%T%T-- freeing procedure for " + xml_table.key_for_iteration.as_lower + "%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%T" + xml_table.key_for_iteration.as_lower + "_progress := 0%N") Result.append_string ("%T%T%T" + xml_table.key_for_iteration.as_lower + "_is_loaded := false%N") Result.append_string ("%T%T%Tall_is_loaded := false%N") from xml_table.item_for_iteration.start until xml_table.item_for_iteration.after loop check_for_attributes (<<"name">>, xml_table.item_for_iteration.item_for_iteration) if attributes_ok then Result.append_string ("%T%T%T" + xml_table.item_for_iteration.item_for_iteration.item("name") + " := void%N") end xml_table.item_for_iteration.forth end Result.append_string ("%T%Tend") Result.append_string("%N%N") xml_table.forth end end getter_code: STRING is require not has_error xml_table /= void do create Result.make_empty Result.append_string ("feature -- Getter%N%N") -- Get Bitmap Result.append_string ("%Tget_bitmap (a_path: STRING): EM_BITMAP is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfile_loader.reset_error%N") Result.append_string ("%T%T%Tfile_loader.load_bitmap (a_path)%N") Result.append_string ("%T%T%Tif not file_loader.has_error then%N") Result.append_string ("%T%T%T%TResult := file_loader.last_bitmap%N") Result.append_string ("%T%T%Telse%N") Result.append_string (" %T%T%T%Thas_error := true%N") Result.append_string (" %T%T%T%Terror_files.put_first (a_path)%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N%N") -- Get Animation Result.append_string ("%Tget_animation (a_path: STRING): EM_ANIMATION is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfile_loader.reset_error%N") Result.append_string ("%T%T%Tfile_loader.load_animation (a_path)%N") Result.append_string ("%T%T%Tif not file_loader.has_error then%N") Result.append_string ("%T%T%T%TResult := file_loader.last_animation%N") Result.append_string ("%T%T%Telse%N") Result.append_string (" %T%T%T%Thas_error := true%N") Result.append_string (" %T%T%T%Terror_files.put_first (a_path)%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N%N") -- Get Sprite Result.append_string ("%Tget_sprite (a_path: STRING): EM_SPRITE is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfile_loader.reset_error%N") Result.append_string ("%T%T%Tfile_loader.load_sprite (a_path, 0, 0)%N") Result.append_string ("%T%T%Tif not file_loader.has_error then%N") Result.append_string ("%T%T%T%TResult := file_loader.last_sprite%N") Result.append_string ("%T%T%Telse%N") Result.append_string (" %T%T%T%Thas_error := true%N") Result.append_string (" %T%T%T%Terror_files.put_first (a_path)%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N%N") -- Get Sound Result.append_string ("%Tget_sound (a_path: STRING): EM_SOUND is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfile_loader.reset_error%N") Result.append_string ("%T%T%Tfile_loader.load_sound_file (a_path)%N") Result.append_string ("%T%T%Tif not file_loader.has_error then%N") Result.append_string ("%T%T%T%TResult := file_loader.last_sound%N") Result.append_string ("%T%T%Telse%N") Result.append_string (" %T%T%T%Thas_error := true%N") Result.append_string (" %T%T%T%Terror_files.put_first (a_path)%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N%N") -- Get Music Result.append_string ("%Tget_music (a_path: STRING): EM_MUSIC is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfile_loader.reset_error%N") Result.append_string ("%T%T%Tfile_loader.load_music_file (a_path)%N") Result.append_string ("%T%T%Tif not file_loader.has_error then%N") Result.append_string ("%T%T%T%TResult := file_loader.last_music%N") Result.append_string ("%T%T%Telse%N") Result.append_string (" %T%T%T%Thas_error := true%N") Result.append_string (" %T%T%T%Terror_files.put_first (a_path)%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N%N") -- Get Bitmap font Result.append_string ("%Tget_bmp_font (a_path: STRING): EM_BMP_FONT is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfile_loader.reset_error%N") Result.append_string ("%T%T%Tfile_loader.load_bmp_font (a_path)%N") Result.append_string ("%T%T%Tif not file_loader.has_error then%N") Result.append_string ("%T%T%T%TResult := file_loader.last_bmp_font%N") Result.append_string ("%T%T%Telse%N") Result.append_string (" %T%T%T%Thas_error := true%N") Result.append_string (" %T%T%T%Terror_files.put_first (a_path)%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N%N") -- Get TTF Font Result.append_string ("%Tget_ttf_font (a_path: STRING; a_point_size: INTEGER): EM_COLOR_TTF_FONT is%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Tfile_loader.reset_error%N") Result.append_string ("%T%T%Tfile_loader.load_ttf_font (a_path, a_point_size)%N") Result.append_string ("%T%T%Tif not file_loader.has_error then%N") Result.append_string ("%T%T%T%TResult := file_loader.last_ttf_font%N") Result.append_string ("%T%T%Telse%N") Result.append_string (" %T%T%T%Thas_error := true%N") Result.append_string (" %T%T%T%Terror_files.put_first (a_path)%N") Result.append_string ("%T%T%Tend%N") Result.append_string ("%T%Tend%N%N") end error_handling_code: STRING is require not has_error xml_table /= void do create Result.make_empty Result.append_string ("feature -- Error handling%N%N") Result.append_string ("%Thas_error: BOOLEAN%N%N") Result.append_string ("%Treset_error is%N") Result.append_string ("%T %T%T-- set 'has_error' to false%N") Result.append_string ("%T%Tdo%N") Result.append_string ("%T%T%Thas_error := false%N") Result.append_string ("%T%Tend%N%N") Result.append_string ("%Terror_files: DS_LINKED_LIST[STRING]%N%N") end footer_code: STRING is require not has_error do create Result.make_empty -- end of class Result.append_string ("end%N") end feature -- helper functions print_help is -- print help for the command line tool do io.put_string ("usage: " + cropped_program_name (arguments.program_name) + " input_file.xml [-o output_class_name] [--no-singleton]%N") io.put_string ("-o, --output%T%Tspecify the name of the generated class%N") io.put_string ("--no-singleton%T%Tdo not create a singleton class%N") io.put_string ("-h, --help%T%Tshow this usage message%N") end feature -- Attributes default_point_size: INTEGER is 15 end