Download Files using a For Each Loop - file

Please keep in mind that I am a TOTAL NOOB. I am not asking anyone to code my entire project, but I am easily lost in many cases. Your help and patience is much appreciated ...
My AS3 code looks something like this:
var totalFeatureImageDownloads:Number = 0; //total file size of all feature images, in bytes
var totalFeatureImageDownloaded:Number = 0; //total downloaded of all feature images, in bytes
for each(var featureimage:XML in xml.featureimages.imgurl)
{
trace(featureimage);
var featureImageRequest:URLRequest = new URLRequest(featureimage);
var featureImageLoader:URLLoader = new URLLoader(featureImageRequest);
totalFeatureImageDownloads = totalFeatureImageDownloads + featureImageLoader.bytesTotal;
trace(featureImageLoader.bytesTotal);
}
Currently I have 3 images in the XML file, and their URL's are traced properly. However, bytesTotal always equals 0. Also, I know that this loop creates 3 identically named URLRequest's and 3 identically named URLLoaders. I probably need to create 3 differently named ones.
I would like all the images to be downloaded simultaneously, and I need to calculate the combined file size for all three, along with the download status as a fraction (bytesLoaded / bytesTotal).
I have multiple scenarios like this, and am stuck until I get this finished. Your help is much appreciated. Cheers.

It is not possible to read the bytesTotal from a newly created URLLoader, because the client needs to connect to the web server first to get the size of the file to be downloaded. You need to listen for the first ProgressEvent.PROGRESS event sent by the URLLoader, once that event is sent the bytesTotal property is more likely to be accurate. I say "more likely" because it is not possible to get a total file size if the data is dynamically generated on the server, but in the general case where you download a file from the server it works.
I recognize the general scenario. There are libraries to aid with this, such as LoaderMax or BulkLoader. I personally use BulkLoader: https://github.com/arthur-debert/BulkLoader

Related

Lua, Love2d, two games with the same class name in different folders

I'm new to Lua and Love2D, I did 2-3 simple games and I wanted to put them together. I did a window where you choose which game you want to play. It succeed; with a little problem. Two of my games use a ball. So both have a Ball.lua File. I use the require function to load the Ball file in each of my games. It works at first, I can play Game1, go back and play Game2 without any problem. But if I go back and want to play the Game1 again. His ball.lua File will not be required since require only load once. Then there will have an error since my game1 is trying to use my Game2's ball Class.
I wanted to know which solution would be best :
Just rename the files. (I would like to avoid it, feels hardcoding to me)
Use doFile. (I never used it, I don't even know if it would work)
Require the two Ball's Classes in my Main menu and pass it by parameter when loading each game (Don't know if it would work too)
If you want to see my code for more explanation, here's the link : https://github.com/cbelangerstpierre/Games/tree/main/Games
Thanks in advance !
As you know, require will only execute each file once. However it will also save the return value of the file so you can require the file as many times as you want and still get the same value.
In your Ball.lua files, make your Ball declarations local:
local Ball = Class{}
Then at the bottom of those files add:
return Ball
Then, change your main.lua files to store to the global Ball variable:
Ball = require "Atari-Breakout.Ball"
and
Ball = require("Switching-Ball.Ball")
Ideally, it's recommended to make all of your variables local and return tables from the files that you need to require.

The Basics of Parse Blocks in Swift

I'm having a little issue on understanding how exactly blocks work.
for x in self.activerestaurantIDArray
{
let namelabel = x.0
self.activenameArray.append(namelabel)
let distancelabel = x.1
self.activedistanceArray.append(distancelabel)
let imageFile = x.2
imageFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let realimage = UIImage(data: imageData!)
self.activeimageArray.append(realimage!)
}
println(self.activenameArray)
println(self.activedistanceArray)
println(self.activeimageArray)
})
}
In the code above, I am appending information to an array from the tuple (named: activerestaurantIDArray) so that I will get separate arrays of name, distance and image. As for the image, I can only retrieve a PFFile from parse so I will have to transform the file into a UIImage.
However, when I do this, the appends for the activeImageArray is in effect only when the println() is within the Block (imageFile.getDataInBackgroundWithBlock).
If I were to println(self.activeimageArray) at any place outside of the box, the array will turn out nil. I am not really sure why that happens or how I should go about ensuring that the appended values carry out beyond the Block{}. Any help would be greatly appreciated.
Closures (also known as yes, blocks) are pieces of code that can be called later, they are essentially functions.
What you are doing in imageFile.getDataInBackgroundWithBlock is to download the data, and when the data is downloaded, imageFile.getDataInBackgroundWithBlock will call your block, passing you the contents of the thing it just downloaded.
It is done this way because downloading must take place in a separate thread. Otherwise your main thread will freeze, and not only will it cause annoyances for the user, but after 5 seconds of non-responsiveness, the OS will kill your app. getDataInBackgroundWithBlock spawns a different thread for the download operation, and then it lets you know by calling your block. This allows you to download anything without "freezing" or "hanging" your app.
So imageData, when outside the block, is nil because you are trying to use it before the block has completed the download operation. Your code will never run slower than a download operation, so you must use the block to implement the code you want to use when an operation finishes downloading, in this case, setting the imageData.
Edit to add, this is not exclusive to Parse. Almost any framework that can freeze your UI will do those operations and implement the notifications using blocks. It's still possible to be notified by using delegates, but ever since blocks were introduces in Objective-C, people have been moving away from that pattern.

escaping a complete packet read in apache module

According to this site:
http://pedrowa.weba.sk/docs/ApiDoc/apidoc_ap_get_client_block.html
This function:
ap_get_client_block(request_rec *r, char *buffer, int bufsiz);
Reads a chunk of POST data coming in from the network when a user requests a webpage.
So far, I have the following core code that reads the data:
while (len_read > 0){
len_read=ap_get_client_block(r,argsbuffer,length);
if((rpos+len_read) > length){rsize=length-rpos;}else{rsize=len_read;}
memcpy((char*)*rbuf+rpos,(char*)argsbuffer,(size_t)rsize);rpos+=rsize;
}
argsbuffer is a character array of length bytes and rbuf is a valid pointer, and the rest of the variables are apr_off_t data type.
If I changed this code to this:
while (len_read > 0){
len_read=ap_get_client_block(r,argsbuffer,length);
if((rpos+len_read) > length){rsize=length-rpos;}else{rsize=len_read;}
memcpy((char*)*rbuf+rpos,(char*)argsbuffer,(size_t)rsize);rpos+=rsize;
if (rpos > wantedlength){len_read=0;}
}
would I be able to close the stream some way and maintain processing speed without corrupt data coming in?
I already executed ap_setup_client_block(r, REQUEST_CHUNKED_ERROR) and made sure ap_should_client_block(r) returned true before processing the first code above. so that is like in a sense, opening a file. ap_get_client_block(r,argsbuffer,length). is like reading a file. Now what about an some ap_ command equivalent to close?
What I want to avoid is corrupt data.
The data that is incoming is in various sizes and I only want to attempt to capture a certain piece of data without having a loop go through the entire set of data every time. Thats why I posted this question.
For example: If I wanted to look for "A=123" as the input data within the first fixed 15 bytes and the first set of data is something like:
S=1&T=2&U=35238952958&V=3468348634683963869&W=fjfdslhjsdflhjsldjhsljsdlkj
Then I want the program to examine only:
S=1&T=2&U=35238
I'd be tempted using the second block of code. The first block of code works but goes through everything.
Anyone have any idea? I want this to execute on a live server as I am improving a security detection system. If anyone knows any other functionality that I should add or remove to my code, let me know. I want to optimize for speed.

Ansi C dynamic include

I was assigned to edit part of Ansi C application but my knowledge of pure C is just basics. Anyway current situation is I have map1_data1.h, map1_data2.h, map2_data1.h, map2_data2.h and variables in those files are always connected to the map name = map1_structure in map1_data1.h and so on.
In app there is #include for each file and in code then something like
if (game->map == 1){
mapStructure = map1_structure
} else {
mapStructure = map2_structure
}
I have to extend this to be able to load the map dynamicly so something like
void loadMap(int mapId){
mapStructure = map*mapId*_structure // just short for what i want to achieve
}
My first idea to do so was removing map name connection in variables name in map1_data.h and have just structure variable in there. That requires only one header file at time to be loaded and thats where I'm stucked. Havent found any clues to do so on google.
I would like to have it as variable as possible so something like #include "map*mapId*_data1.h" but should be ok to have 1 switch in one place in whole app to decide on what map to be loaded.
One more thing, the app keeps running for more than 1 game = it will load various maps in one run.
Judging from the comments, you have a single type, call it Map, which is a structure type containing a collection of different data types, including 3D arrays and points and so on. You need to have some maps built into the program; later on, you will need to load new maps at runtime.
You have two main options for the runtime loading the maps:
Map in shared object (shared library, dynamically loaded library, aka DLL).
Map in data file.
Of these two, you will choose the data file over the shared object because it is, ultimately, simpler and more flexible.
Shared Object
With option 1, only someone who can compile a shared library can create the new maps. You'd have a 'library' consisting of one or more data objects, which can be looked up by name. On most Unix-like systems, you'd end up using dlopen() to load the library, and then dlsym() to find the symbol name in that library (specifying the name via a string). If it is present in the library, dlsym() will return you a pointer.
In outline:
typedef void *SO_Handle;
const char *path_to_library = "/usr/local/lib/your_game/libmap32.so";
const char *symbol_name = "map32_structure";
SO_Handle lib = dlopen(path_to_library, RTLD_NOW);
if (lib == 0)
...bail out...
map_structure = dlsym(lib, symbol_name);
if (map_structure == 0)
...bail out...
You have to have some way of generating the library name based on where the software is installed and where extensions are downloaded. You also have to have some way of knowing the name of the symbol to look for. The simplest system is to use a single fixed name (map_structure), but you are not constrained to do that.
After this, you have your general map_structure read for use. You can invent endless variations on the theme.
Data file
This is the more likely way you'll do it. You arrange to serialize the map structure into a disk file that can be read by your program. This will contain a convenient representation of the data. You should consider the TLV (type-length-value) encoding scheme, so that you can tell by looking at the type what sort of data follows, and the length tells you how many of them, and the value is the data. You can do this with binary data or with text data. It is easier to debug text data because you can look at and see what's going on. The chances are that the difference in performance between binary and text is small enough (swamped by the I/O time) that using text is the correct way to go.
With a text description of the map, you'd have information to identify the file as being a map file for your game (perhaps with a map format version number). Then you'd have sections describing each of the main elements in the Map structure. You'd allocate the Map (malloc() et al), and then load the data from the file into the structure.

Printing multiply copies of Word document from WPF/C# .NET 4

I'm building an WPF application in C# and .NET 4 and need to print out two copies of the same file.
I have the following code, which gets the job done, but it's not that pretty as Word opens up twice.
Process myProcess = new Process();
myProcess.StartInfo.FileName = invoiceFileAbsoluteStoreagePath;
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
//Print out two copies - ugly! - no better way apparently
myProcess.Start();
myProcess.WaitForExit();
//Let's do it again... there need to be a better way :(
myProcess.Start();
myProcess.WaitForExit();
Can't I somehow send an argument to Word letting it know that I want X copies from this document?
I usually do my printing via WPF flow documents. From there you can just create the WPF page, invoke the print command on it, auto fill the number of copies and execute the print job. If you only have a few documents you can recreate them in XAML and even inject data into them. It is a little time consuming to start, but once implemented, it is quite fast. No opening word. Just opening a xml file as a XamlDocument, inject data via ViewModel, and printing.
While this is not specifically about flow documents, it will get you there.

Resources