Sign in to follow this  
Drew_Benton

Unity [.net] First C# Program - GameDev Thread Saver

Recommended Posts

Drew_Benton    1861
I've recently started taking a look at C# since it's been on my todo list for so long. And what better way to start learning it with other than making tools! This C# program is called Thread Saver and what it does is you give it an id of the GameDev.net thread you want to save, and it will save the entire thread, all pages, into one html file to your hard drive. Now you will have to look at the code to see everything I did. It is very brekable right now, but anything that does popup with specific threads could be fixed with more code. One of the major things that I added in was the resolving of all relative links to absolute links, so when you save the page, it looks like a gamedev page rather than what you get when you save it from your browser. Here's the complete source, since I have no reason to keep it closed. There are lots of room for improvements and modifications. If you uncomment out a lot of the stuff in the CombineGameDevThread function, then you will end up with a nice page that is print ready! Since there are a lot of slashes and tags the forum might eat, I will just post the file instead. Now for my C# questions/comments from making this: 1. Is the StreamReader.ReadToEnd(); function used in a good sense how I did it? Is there anything that would be faster? I tried using the StreamReader.Read function, but I could not figure out how to convert from a char[] to a string. 2. How do you convert from a char[] to a string? The .toString method returns simply System.char[] or whatever that string is for the object name. 3. Why, oh why do functions such as Replace, Insert, etc.. return the new string rather than modify the current! It took me so long to figure out that the new modified string is returned rather than the updated itself. I can see the logics, but darn, that was so confusing! 4. It took me a bit to figure out that .Find is really .IndexOf [headshake] 5. Other than that, does anyone see any improvements that I can make in terms of C# concepts? Ok thanks for any help/suggestions. As for the program, if you want to do some comparing, I saved the thread, Single Word Game, and my results were:
102 Pages
...
Operation time: 358.234 seconds
The final file is 7.962MB and takes quite a bit for FF to load [lol]. I plan to improve this a lot more, but for right now, I'm just learning the concepts. If there's anything you'd like to see in it, feel free to make a suggestion and I will write it down. - Drew

Share this post


Link to post
Share on other sites
intrest86    742
Quote:
Original post by Drew_Benton
Now for my C# questions/comments from making this:

1. Is the StreamReader.ReadToEnd(); function used in a good sense how I did it? Is there anything that would be faster? I tried using the StreamReader.Read function, but I could not figure out how to convert from a char[] to a string.

2. How do you convert from a char[] to a string? The .toString method returns simply System.char[] or whatever that string is for the object name.

3. Why, oh why do functions such as Replace, Insert, etc.. return the new string rather than modify the current! It took me so long to figure out that the new modified string is returned rather than the updated itself. I can see the logics, but darn, that was so confusing!


I'm tired and about to crash, but here is a little help. Strings are immutable objects, meaning that they can't be changed once they are set. Since that is the case,, and function that returns a different string must make a new string. If you are modifying a string a lot, consider using its mutable cousin the StringBuilder. As far as converting char[] to a string, just pass the character array into the string constructor.

Share this post


Link to post
Share on other sites
Washu    7829
Quote:
Original post by Drew_Benton
Now for my C# questions/comments from making this:

1. Is the StreamReader.ReadToEnd(); function used in a good sense how I did it? Is there anything that would be faster? I tried using the StreamReader.Read function, but I could not figure out how to convert from a char[] to a string.

Dunno, would have to read the code to know. [grin]
Quote:

2. How do you convert from a char[] to a string? The .toString method returns simply System.char[] or whatever that string is for the object name.

String constructor
Quote:

3. Why, oh why do functions such as Replace, Insert, etc.. return the new string rather than modify the current! It took me so long to figure out that the new modified string is returned rather than the updated itself. I can see the logics, but darn, that was so confusing!

strings are immutable, aka: they cannot change. Use StringBuilder/StringWriter/StringReader.
Quote:

4. It took me a bit to figure out that .Find is really .IndexOf [headshake]

lol, made sense to me.
Quote:

5. Other than that, does anyone see any improvements that I can make in terms of C# concepts?

Your naming scheme is inconsistant. Stop mixing cases of the method names. If you are going to use upper camel case, then stick with that, mixing lower and upper camel case (pascal case) just confuses things.

It's also not done in a very OO fashion. You might also want to look into the MS HTML processing stuff. You can use it to build a DOM of the pages, find hyperlinks, images, stylesheets. That information could be used to download a whole tree of the pages, based on the information. You are replicating a lot of code that's already been done.

Definantly need to use a StringBuilder, your current code is very....memory inneficient.

Share this post


Link to post
Share on other sites
mutex    1111
If you're interested in efficiency, you might want to check out the CLR Profiler. An article on it is available here. The call graph view is interesting. It'll let you see what functions are taking up the most time.

Regarding your code itself, like Washu I'd recommend naming consistency. I'd also recommend following .NET naming conventions for even more consistency; I've always found it annoying when Java naming conventions sneak into .NET and vice versa. The link is quite in depth; you can find summaries through Google.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Sorry guys for the late replies, I will be getting back to this thread in a week or so when I have my dev computer up again. It's broken now so I am working with an old celeron 633mhz. It's not that bad and Visual C# 2005 Beta > Visual Studio 2003 C# [wink]. I'll start reading all the replies and respond again soon. Thanks for all your time!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this