Articles Archive
Articles Search
Director Wiki
 

Updating Director Movies Delivered on CD-ROM, Part 2

March 28, 2002
by Will Turnage

In Part 1, you learned about the requirements for having a CD-ROM project with updateable content. Then you learned how to check the date of a local file and if the file was out of date, replace it with a newer one on the Web. While this method will work fine if your content has a strict release schedule, it's not ideal for all situations. If you want to update your content immediately then there's no way to support this. In this installment, you'll learn how to alleviate this issue by synchronizing the contents of a local folder with the contents of a folder online.

To accomplish this synchronization, you start off by getting a list of files and modification dates for the folder that's online. Next, you get a list of files and modification dates in your local folder. Once you have these two lists, then you compare the two. If there's a newer file on the server, then you download that file. This way, if you only update one section of your program, the user doesn't have to re-download all of the content again.

The first step in your code is to step outside of Director for a bit, and to write a script that will return the contents of your online folder along with their modification dates. This script will reside in the same folder online as the rest of your files. You could write this script in many different languages like Perl or Python, but in this case, you'll use PHP. Just create a PHP file and name it filelist.php. Then insert this code into the file.

<?php
  $handle = opendir (".");
  while (false !== ($file = readdir ($handle))) {
    if ($file != "." && $file != ".." && $file != "filelist.php") {
        echo $file . " " . date ("Ymd", filemtime ($file)) . "\n";
    }
  }
?>

This code starts by opening the current directory and storing the reference to that directory in a variable called $handle. Next you loop through each file in the directory. For each file, you check to make sure that its value is not ".", "..", or "filelist.php" which is the name of your script. If the file name isn't one of these three things, then you print the name of the file along with its modification date. Note that the format you give the modification date is an eight character string with the first four characters being the year, the next two characters being the month, and the last two characters being the day. This is designed for easier compatibility with Director's date object.

Once you've created this script and uploaded it to your server, then all you have to do is use Director's getNetText command to download a list of all of your online files with their modification dates.

getNetText ("http://www.myserver.com/filelist.php")

Once you have the netTextResult from this operation, then you should process the text and place it into a property list.

on processRemoteFileListing me, theText
  pRemoteFileList = [:]
  counter = theText.line.count
  repeat with i = 1 to counter
    tempFileName = theText.line[i].word[1]
    if tempFileName <> EMPTY then
      tempDate = date (theText.line[i].word[2])
      pRemoteFileList.addProp (tempFileName, tempDate)
    end if
  end repeat
end

This handler starts by being passed the results of your getNetText operation. In this case, the result is in a parameter named theText. You begin by initializing a property list called pRemoteFileList. Next you determine the number of lines in the text string and loop through each line in the string. The first word of each line contains the file's name, so you start by putting that file name into a variable. Next you check to make sure that the file name isn't empty, and if so, then you create a date object using the modification date of the file. Finally, you add the file name and modification date to your property list.

Next, you're ready to create the same type of list, only this time you'll use your local files instead of the files online. This won't involve any PHP, just some Lingo and a little bit of the FileXtra3. This handler is very similar to the handler you wrote last week, the biggest difference being that this handler is designed to loop through a folder of files, instead of just looking at one specific file.

pLocalFileList = [:]
fileObj = new (xtra "fileXtra3")
monthList = ["Jan":"01", "Feb": "02", "Mar":"03", "Apr": "04", ""May": "05", "Jun": "06", "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"]
if the platform contains "Windows" then
  tempPath = the moviePath & "datafiles\"
else
  tempPath = the moviePath & "datafiles:""
end if

You start by creating your empty property list. Next, you create an instance of the FileXtra3 and also create another list called monthList, which will be used later to calculate dates. Finally, you create a variable which will store the full path to your data, while making sure that the path is coded correctly for the specific platform you program is running on. Once you've initialized your variables, then you're ready to build your file list.

repeat with i = 1 to 10000
  tempFileName = getNthFileNameInFolder (tempPath, i)
  if tempFileName = EMPTY then
    exit repeat
  else
    fileModDate = fileObj.fx_FileGetModDate (tempPath & tempFileName)
    if fileModDate <> EMPTY then
      tempYear = fileModDate.char[21..24]
      tempMonth = monthList[fileModDate.char[5..7]]
      tempDay = fileModDate.char[9..10]
      tempFileDate = date (tempYear & tempMonth & tempDay)
    else
      tempFileDate = the systemDate
    end if
    pLocalFileList.addProp (tempFileName, tempFileDate)
  end if
end repeat
fileObj = 0

You loop through each of the files in your folder. For each file, you check its modification date, and if it exists, then you parse that value into a month, day, and year. Using those values you calculate a date object for that specific day. If there was an error, or no modification date was returned, then you just use the current date. Once you have the date, then you add the date and the file name to your local property list.

When you are done, you will have a list of all the files you have in your local folder along with their modification dates. The last step is that you will need to compare the two folders and create a third list that will contain just the files that you need to upload.

global gFilesToUpdate

on localFilesAreUpToDate me
  gFilesToUpdate = []
  repeat with i = 1 to pRemoteFileList.count
    if voidP (pLocalFileList.getAProp(pRemoteFileList.getPropAt(i))) then
      gFilesToUpdate.append(pRemoteFileList.getPropAt(i))
    else
      if pRemoteFileList[i] > pLocalFileList[pRemoteFileList.getPropAt(i)] then
        gFilesToUpdate.append(pRemoteFileList.getPropAt(i))
      end if
    end if
  end repeat
end

This handler begins by setting the global variable gFilesToUpdate to an empty list. Next you loop through your list of files that are in your online folder. First you check to see if the online file even exists in your local folder. If it doesn't, then you add it to gFilesToUpdate. If the file does exist in the local folder, then you compare the dates of the two files. If the online file is newer, then you add the file name to gFilesToUpdate. When the repeat loop has finished, you will have a global list containing all of the files that you need to download. If the list is still empty, then all of your local files are up to date, and you don't need to download anything. But if the list is full, then you'll have to tell the user that they need to download the latest files.

A sample Director 8 movie is available in SIT or ZIP archives The demo includes some sample casts and a PHP script file. The demo movie checks a folder on my personal server for new files.

Luckily, there is an easy approach, but it will involve delving into some server-side programming like the PHP script above. Ideally, you should put all the data that is updateable into a single folder on your web server.

All colorized Lingo code samples have been processed by Dave Mennenoh's brilliant HTMLingo Xtra, available from his site at http://www.crackconspiracy.com/~davem/

Will Turnage is a multimedia programmer based in New York City. In addition to his weekly role as Director Online's Multimedia Handyman, he is also the Technology Director for Sony Music's Client Side Technologies Group. You can read more about his work at http://will.turnage.com/.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.