• R/O
  • SSH
  • HTTPS

eirsamples: Commit


Commit MetaInfo

Revision36 (tree)
Time2020-10-19 04:40:48
Authorquiret

Log Message

- added the ability to replay commands using the "load" command

Change Summary

Incremental Difference

--- rwchunk-writer/src/main.cpp (revision 35)
+++ rwchunk-writer/src/main.cpp (revision 36)
@@ -1,6 +1,7 @@
11 #include <renderware.h>
22
33 #include <iostream>
4+#include <fstream>
45 #include <string>
56
67 #include <sdk/NumericFormat.h>
@@ -26,7 +27,7 @@
2627 {
2728 char c = *cmdline_str;
2829
29- if ( c == ' ' || c == '\t' )
30+ if ( c == ' ' || c == '\t' || c == '\r' )
3031 {
3132 if ( chars != nullptr )
3233 {
@@ -81,75 +82,30 @@
8182 return LibraryVersion( 3, 6, 0, 3 );
8283 }
8384
84-int rwmain( rw::Interface *rwEngine )
85+static const rwStaticMap <rwStaticString <char>, uint32, lexical_string_comparator <false>> string_to_chunk_id =
8586 {
86- std::cout << "RenderWare chunk writer by (c)The_GTA, turningtides@outlook.de" << std::endl;
87- std::cout << "compiled on " __DATE__ << std::endl;
88- std::cout << std::endl;
87+ { "struct", CHUNK_STRUCT },
88+ { "string", CHUNK_STRING },
89+ { "texture_native", CHUNK_TEXTURENATIVE },
90+ { "tex_native", CHUNK_TEXTURENATIVE }
91+};
8992
90- static rwStaticMap <rwStaticString <char>, uint32, lexical_string_comparator <false>> string_to_chunk_id =
93+struct chunkerAPI
94+{
95+ AINLINE chunkerAPI( rw::Stream *filestream ) : bitbuf( nullptr, 0, bufalloc )
9196 {
92- { "struct", CHUNK_STRUCT },
93- { "string", CHUNK_STRING },
94- { "texture_native", CHUNK_TEXTURENATIVE },
95- { "tex_native", CHUNK_TEXTURENATIVE }
96- };
97+ this->rwEngine = filestream->getEngine();
98+ this->filestream = filestream;
99+ }
97100
98- while ( true )
101+ void parseAllCommands( std::istream& input )
99102 {
100- std::cout << "Enter a filename: ";
101-
102- std::string filename;
103-
104- std::cin >> filename;
105-
106- if ( filename == "exit" )
107- break;
108-
109- // Create a file with that name.
110- rw::streamConstructionFileParam_t fileparam( filename.c_str() );
111-
112- rw::StreamPtr filestream = rwEngine->CreateStream( RWSTREAMTYPE_FILE, RWSTREAMMODE_CREATE, &fileparam );
113-
114- if ( filestream.is_good() == false )
115- {
116- std::cout << "could not open file" << std::endl;
117-
118- continue;
119- }
120-
121- rwStaticVector <BlockProvider> active_blocks;
122-
123- BlockProvider current_block;
124-
125- // For struct chunks we provide a bitstream API.
126- size_t current_block_bit_offset = 0;
127- BasicMemStream::basicMemStreamAllocMan <size_t> bufalloc;
128- BasicMemStream::basicMemoryBufferStream <size_t> bitbuf( nullptr, 0, bufalloc );
129-
130- // Configuration.
131- bool enable_alignments = false;
132-
133- auto flush_bits = [&]( void )
134- {
135- if ( current_block_bit_offset == 0 )
136- return;
137-
138- size_t bits_to_byte_bound = eir::getBitCountToByteBound( current_block_bit_offset - 1 );
139-
140- eir::setBits( bitbuf.Data(), false, current_block_bit_offset, bits_to_byte_bound );
141-
142- current_block.write( bitbuf.Data(), bitbuf.Size() );
143- bitbuf.Truncate( 0 );
144- current_block_bit_offset = 0;
145- };
146-
147103 // Go into command-mode.
148- while ( true )
104+ while ( input.eof() == false )
149105 {
150106 std::string cmd;
151107
152- std::getline( std::cin, cmd );
108+ std::getline( input, cmd );
153109
154110 // Parse into tokens.
155111 auto tokens = parse_cmdline( cmd );
@@ -279,6 +235,30 @@
279235 std::cout << "not enough params (lmaj, lmin, rmaj, rmin)" << std::endl;
280236 }
281237 }
238+ else if ( BoundedStringEqual( maincmd.GetConstString(), maincmd.GetLength(), "load", false ) )
239+ {
240+ recognized_cmd = true;
241+
242+ if ( tokens.GetCount() >= 2 )
243+ {
244+ std::fstream newcmdfile( tokens[1].GetConstString(), std::ios::in | std::ios::binary );
245+
246+ if ( newcmdfile.good() )
247+ {
248+ this->parseAllCommands( newcmdfile );
249+
250+ std::cout << "done parsing command file" << std::endl;
251+ }
252+ else
253+ {
254+ std::cout << "failed to open command file" << std::endl;
255+ }
256+ }
257+ else
258+ {
259+ std::cout << "missing file name" << std::endl;
260+ }
261+ }
282262 else if ( current_block.inContext() && current_block.getBlockID() == CHUNK_STRUCT )
283263 {
284264 if ( BoundedStringEqual( maincmd.GetConstString(), maincmd.GetLength(), "bits", false ) )
@@ -567,11 +547,77 @@
567547
568548 if ( recognized_cmd == false )
569549 {
570- std::cout << "unknown command" << std::endl;
550+ std::string cmdName( tokens[0].GetConstString(), tokens[0].GetLength() );
551+
552+ std::cout << "unknown command: " << cmdName << std::endl;
571553 }
572554 }
573555 }
556+ }
574557
558+ void flush_bits( void )
559+ {
560+ if ( current_block_bit_offset == 0 )
561+ return;
562+
563+ size_t bits_to_byte_bound = eir::getBitCountToByteBound( current_block_bit_offset - 1 );
564+
565+ eir::setBits( bitbuf.Data(), false, current_block_bit_offset, bits_to_byte_bound );
566+
567+ current_block.write( bitbuf.Data(), bitbuf.Size() );
568+ bitbuf.Truncate( 0 );
569+ current_block_bit_offset = 0;
570+ }
571+
572+ rw::Interface *rwEngine;
573+ rw::Stream *filestream;
574+
575+ rwStaticVector <BlockProvider> active_blocks;
576+
577+ BlockProvider current_block;
578+
579+ // For struct chunks we provide a bitstream API.
580+ size_t current_block_bit_offset = 0;
581+ BasicMemStream::basicMemStreamAllocMan <size_t> bufalloc;
582+ BasicMemStream::basicMemoryBufferStream <size_t> bitbuf;
583+
584+ // Configuration.
585+ bool enable_alignments = false;
586+};
587+
588+int rwmain( rw::Interface *rwEngine )
589+{
590+ std::cout << "RenderWare chunk writer by (c)The_GTA, turningtides@outlook.de" << std::endl;
591+ std::cout << "compiled on " __DATE__ << std::endl;
592+ std::cout << std::endl;
593+
594+ while ( true )
595+ {
596+ std::cout << "Enter a filename: ";
597+
598+ std::string filename;
599+
600+ std::cin >> filename;
601+
602+ if ( filename == "exit" )
603+ break;
604+
605+ // Create a file with that name.
606+ rw::streamConstructionFileParam_t fileparam( filename.c_str() );
607+
608+ rw::StreamPtr filestream = rwEngine->CreateStream( RWSTREAMTYPE_FILE, RWSTREAMMODE_CREATE, &fileparam );
609+
610+ if ( filestream.is_good() == false )
611+ {
612+ std::cout << "could not open file" << std::endl;
613+
614+ continue;
615+ }
616+
617+ chunkerAPI chunker( filestream );
618+
619+ chunker.parseAllCommands( std::cin );
620+
575621 // File will be closed and flushed here.
576622 }
577623
Show on old repository browser